@bjoernrochel/sevro 0.1.0-rc.8 → 1.0.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 (228) hide show
  1. package/CONTRIBUTING.md +22 -6
  2. package/README.md +5 -1
  3. package/docs/README.md +19 -15
  4. package/docs/architecture.md +138 -17
  5. package/docs/assets/README.md +5 -4
  6. package/docs/builtin-graders-v1.md +9 -2
  7. package/docs/codex-host.md +155 -57
  8. package/docs/contracts.md +2 -1
  9. package/docs/creating-cases.md +9 -2
  10. package/docs/development-cli.md +187 -19
  11. package/docs/documentation-quality.md +12 -2
  12. package/docs/evaluation-workflow.md +110 -0
  13. package/docs/extension-protocol-v1.md +26 -3
  14. package/docs/getting-started.md +23 -2
  15. package/docs/guide-evaluation.md +33 -1
  16. package/docs/identity-v1.md +25 -2
  17. package/docs/installing.md +11 -11
  18. package/docs/native-controls.md +76 -5
  19. package/docs/native-hosts.md +196 -7
  20. package/docs/reading-results.md +102 -0
  21. package/docs/recipes.md +489 -0
  22. package/docs/releases.md +31 -12
  23. package/docs/report-v1.md +29 -1
  24. package/docs/results-v1.md +122 -4
  25. package/docs/run-settings-v1.md +147 -0
  26. package/docs/runtime-v1.md +255 -22
  27. package/docs/troubleshooting.md +12 -12
  28. package/docs/typescript-quality.md +89 -26
  29. package/examples/recipes/candidate.ts +26 -0
  30. package/examples/recipes/cases/coding-change.json +21 -0
  31. package/examples/recipes/cases/host-tools.json +21 -0
  32. package/examples/recipes/cases/mise.json +21 -0
  33. package/examples/recipes/cases/node.json +21 -0
  34. package/examples/recipes/cases/python-uv.json +21 -0
  35. package/examples/recipes/cases/semantic.json +17 -0
  36. package/examples/recipes/coding-change/check.ts +23 -0
  37. package/examples/recipes/extension-artifacts.ts +65 -0
  38. package/examples/recipes/extension-methods.ts +174 -0
  39. package/examples/recipes/extension.ts +37 -0
  40. package/examples/recipes/repository-skill/.agents/skills/startup-guide/SKILL.md +7 -0
  41. package/examples/recipes/repository-skill/.claude/skills/startup-guide/SKILL.md +7 -0
  42. package/examples/recipes/repository-skill/README.md +3 -0
  43. package/examples/recipes/run-inputs.ts +188 -0
  44. package/examples/recipes/run-options.ts +131 -0
  45. package/examples/recipes/run.ts +63 -0
  46. package/examples/recipes/runtime/empty.json +1 -0
  47. package/examples/recipes/runtime/goals.json +1 -0
  48. package/examples/recipes/runtime/mise.json +6 -0
  49. package/examples/recipes/runtime/path.json +1 -0
  50. package/examples/recipes/runtime/plugins.json +4 -0
  51. package/examples/recipes/runtime/python-uv.json +10 -0
  52. package/examples/recipes/semantic-judge.ts +29 -0
  53. package/examples/templates/coding/adapter.ts +17 -0
  54. package/examples/templates/coding/case.json +15 -0
  55. package/examples/templates/repository-skill/.agents/skills/startup-guide/SKILL.md +6 -0
  56. package/examples/templates/repository-skill/extension-data.ts +64 -0
  57. package/examples/templates/repository-skill/extension.ts +31 -0
  58. package/examples/templates/response/adapter.ts +12 -0
  59. package/examples/templates/response/case.json +13 -0
  60. package/package.json +11 -5
  61. package/schemas/report-v1.schema.json +9 -0
  62. package/schemas/run-evidence-v1.schema.json +2 -1
  63. package/schemas/runtime-v1.schema.json +43 -7
  64. package/src/advisory.ts +2 -2
  65. package/src/candidate-artifacts.ts +42 -11
  66. package/src/candidate-codex-state.ts +185 -0
  67. package/src/case-validation.ts +73 -50
  68. package/src/cli/cancellation.ts +20 -0
  69. package/src/cli/command.ts +87 -0
  70. package/src/cli/completion.ts +15 -0
  71. package/src/cli/demo.ts +60 -0
  72. package/src/cli/evaluation.ts +55 -0
  73. package/src/cli/extension.ts +79 -0
  74. package/src/cli/help.ts +198 -0
  75. package/src/cli/hosts.ts +55 -0
  76. package/src/cli/init-settings.ts +30 -0
  77. package/src/cli/init.ts +111 -0
  78. package/src/{cli-invocation.ts → cli/invocation.ts} +214 -218
  79. package/src/cli/loading.ts +151 -0
  80. package/src/cli/preparation.ts +154 -0
  81. package/src/cli/presentation.ts +93 -0
  82. package/src/cli/progress.ts +6 -0
  83. package/src/cli/run-manifest.ts +128 -0
  84. package/src/cli/run-options.ts +403 -0
  85. package/src/cli/run-overrides.ts +69 -0
  86. package/src/cli/settings.ts +154 -0
  87. package/src/cli/summary.ts +43 -0
  88. package/src/cli.ts +13 -591
  89. package/src/contract-types.ts +94 -0
  90. package/src/engine.ts +9 -5
  91. package/src/evaluation/advisory.ts +150 -0
  92. package/src/{evaluation-check-plan.ts → evaluation/check-plan.ts} +103 -67
  93. package/src/evaluation/collection.ts +88 -0
  94. package/src/evaluation/context.ts +10 -0
  95. package/src/{evaluation-grading-host.ts → evaluation/grading-host.ts} +5 -5
  96. package/src/evaluation/grading.ts +235 -0
  97. package/src/{evaluation-invocation-plan.ts → evaluation/invocation-plan.ts} +147 -58
  98. package/src/{evaluation-plan.ts → evaluation/plan.ts} +208 -141
  99. package/src/evaluation/progress.ts +19 -0
  100. package/src/evaluation/resources.ts +241 -0
  101. package/src/evaluation/run.ts +206 -0
  102. package/src/{evaluation-semantic.ts → evaluation/semantic.ts} +77 -87
  103. package/src/evaluation/trial.ts +800 -0
  104. package/src/{evaluation-types.ts → evaluation/types.ts} +79 -29
  105. package/src/evidence/host-results.ts +85 -0
  106. package/src/evidence/retained-artifacts.ts +18 -0
  107. package/src/evidence/trial-writer.ts +203 -0
  108. package/src/evidence/usage.ts +17 -0
  109. package/src/extension-client.ts +34 -96
  110. package/src/extension-session.ts +24 -48
  111. package/src/{advisory-fixture.ts → fixtures/advisory.ts} +12 -72
  112. package/src/{fixture-bin.ts → fixtures/bin.ts} +1 -1
  113. package/src/{fixture-cleanup.ts → fixtures/cleanup.ts} +13 -7
  114. package/src/fixtures/create.ts +183 -0
  115. package/src/{generated-fixture.ts → fixtures/generated.ts} +10 -53
  116. package/src/{git-hooks.ts → fixtures/git-hooks.ts} +1 -1
  117. package/src/{preparation.ts → fixtures/preparation.ts} +6 -16
  118. package/src/{repository-fixture.ts → fixtures/repository.ts} +30 -64
  119. package/src/{fixture-setup.ts → fixtures/setup.ts} +33 -51
  120. package/src/{fixture-tools.ts → fixtures/tools.ts} +1 -1
  121. package/src/{workspace-state.ts → fixtures/workspace-state.ts} +5 -5
  122. package/src/generated/cli-result.d.cts +2 -1
  123. package/src/generated/extension.d.cts +2 -1
  124. package/src/generated/report.cjs +1 -1
  125. package/src/generated/report.d.cts +2 -1
  126. package/src/generated/run-evidence.cjs +1 -1
  127. package/src/generated/run-evidence.d.cts +2 -1
  128. package/src/generated/runtime.cjs +1 -1
  129. package/src/generated/runtime.d.cts +2 -1
  130. package/src/git.ts +93 -0
  131. package/src/graders/git-head.ts +29 -84
  132. package/src/graders/shell.ts +50 -68
  133. package/src/host-diagnostic-redaction.ts +52 -0
  134. package/src/host-diagnostics.ts +331 -0
  135. package/src/hosts/claude-continuation.ts +57 -40
  136. package/src/hosts/claude-credential.ts +160 -63
  137. package/src/hosts/claude-events.ts +34 -18
  138. package/src/hosts/claude-keychain.ts +78 -21
  139. package/src/hosts/claude-login-recovery.ts +85 -0
  140. package/src/hosts/claude-login-refresh.ts +178 -0
  141. package/src/hosts/claude-login-store.ts +162 -0
  142. package/src/hosts/claude-login.ts +107 -0
  143. package/src/hosts/claude-native-goal.ts +15 -12
  144. package/src/hosts/claude-process.ts +10 -50
  145. package/src/hosts/claude-refresh-lock.ts +81 -0
  146. package/src/hosts/claude-repository-invocation.ts +9 -34
  147. package/src/hosts/claude-settings.ts +46 -9
  148. package/src/hosts/claude-skill-activations.ts +169 -0
  149. package/src/hosts/claude-state.ts +28 -15
  150. package/src/hosts/claude-tool-calls.ts +13 -10
  151. package/src/hosts/claude.ts +121 -81
  152. package/src/hosts/codex-app-server.ts +78 -52
  153. package/src/hosts/codex-configuration.ts +5 -10
  154. package/src/hosts/codex-exec-controls.ts +516 -0
  155. package/src/hosts/codex-marketplace.ts +12 -10
  156. package/src/hosts/codex-native-calls.ts +15 -122
  157. package/src/hosts/codex-process.ts +10 -183
  158. package/src/hosts/codex-profile.ts +36 -21
  159. package/src/hosts/codex-session-files.ts +1 -9
  160. package/src/hosts/codex-state.ts +134 -18
  161. package/src/hosts/codex.ts +210 -183
  162. package/src/hosts/isolation-common.ts +8 -11
  163. package/src/hosts/isolation-roots.ts +2 -2
  164. package/src/hosts/linux-sandbox.ts +15 -2
  165. package/src/hosts/mac-sandbox.ts +87 -18
  166. package/src/hosts/native-controls.ts +72 -22
  167. package/src/hosts/native-request.ts +55 -0
  168. package/src/hosts/process-lifecycle.ts +216 -48
  169. package/src/hosts/protected-worktrees.ts +1 -1
  170. package/src/hosts/skill-hook-catalog.ts +131 -0
  171. package/src/hosts/skill-hook-handler.ts +374 -0
  172. package/src/hosts/skill-hooks.ts +577 -0
  173. package/src/instrumentation.ts +7 -8
  174. package/src/native-transcript-state.ts +22 -3
  175. package/src/native-transcript-view.ts +52 -13
  176. package/src/native-transcripts.ts +26 -11
  177. package/src/paths.ts +22 -0
  178. package/src/process-lifecycle.ts +150 -0
  179. package/src/provenance.ts +27 -7
  180. package/src/prune.ts +300 -0
  181. package/src/report.ts +63 -36
  182. package/src/resolved-case.ts +1 -1
  183. package/src/results-lock.ts +42 -0
  184. package/src/results.ts +14 -18
  185. package/src/{runtime-config.ts → runtime/config.ts} +160 -46
  186. package/src/{runtime-discovery-apple.ts → runtime/discovery-apple.ts} +3 -3
  187. package/src/{runtime-discovery-providers.ts → runtime/discovery-providers.ts} +3 -9
  188. package/src/{runtime-discovery.ts → runtime/discovery.ts} +4 -4
  189. package/src/{runtime-hook-processes.ts → runtime/hook-processes.ts} +1 -1
  190. package/src/{runtime-hooks.ts → runtime/hooks.ts} +32 -22
  191. package/src/{runtime-paths.ts → runtime/paths.ts} +5 -19
  192. package/src/{runtime-query.ts → runtime/query.ts} +5 -5
  193. package/src/{runtime-seeds.ts → runtime/seeds.ts} +3 -3
  194. package/src/runtime/shared-cache.ts +130 -0
  195. package/src/{runtime-state.ts → runtime/state.ts} +28 -11
  196. package/src/schema-types.ts +73 -161
  197. package/src/shell.ts +4 -0
  198. package/src/transcript-retention.ts +146 -0
  199. package/src/value-guards.ts +22 -0
  200. package/docs/assets/sevro-logo.png +0 -0
  201. package/docs/assets/sevro-symbol.svg +0 -1
  202. package/docs/assets/sevro-wordmark.svg +0 -1
  203. package/docs/cache-ownership-validation.md +0 -49
  204. package/docs/claude-continuation-validation.md +0 -47
  205. package/docs/concurrency-validation.md +0 -86
  206. package/docs/configuration-root-validation.md +0 -70
  207. package/docs/engine-slice.md +0 -114
  208. package/docs/evidence/runtime-startup-30/README.md +0 -185
  209. package/docs/evidence/runtime-startup-30/after.json +0 -319
  210. package/docs/evidence/runtime-startup-30/baseline-timeout.jsonl +0 -2
  211. package/docs/evidence/runtime-startup-30/before.json +0 -403
  212. package/docs/evidence/runtime-startup-30/consumer-smoke.json +0 -63
  213. package/docs/evidence/runtime-startup-30/full-policy-diagnostic.jsonl +0 -3
  214. package/docs/evidence/runtime-startup-30/minimal-policy-diagnostic.jsonl +0 -3
  215. package/docs/evidence/runtime-startup-30/plugin-template-assessment.json +0 -20
  216. package/docs/evidence/runtime-startup-30/seedless-timeout.jsonl +0 -2
  217. package/docs/evidence.md +0 -17
  218. package/docs/fixture-cleanup-validation.md +0 -73
  219. package/docs/guide-verification.json +0 -239
  220. package/src/evaluation-context.ts +0 -259
  221. package/src/evaluation-fixture.ts +0 -302
  222. package/src/evaluation-run.ts +0 -200
  223. package/src/evaluation-trial.ts +0 -1016
  224. package/src/hosts/claude-nested-skills.ts +0 -272
  225. package/src/hosts/codex-command-output.ts +0 -510
  226. package/src/hosts/codex-executor-command.ts +0 -291
  227. package/src/hosts/codex-skill-reads.ts +0 -783
  228. /package/src/{trial-scheduler.ts → evaluation/scheduler.ts} +0 -0
@@ -1,4 +1,4 @@
1
- import { runtimeSeedSources } from "../runtime-config";
1
+ import { runtimeLocalHttpEnabled, runtimeSeedSources } from "../runtime/config";
2
2
  import {
3
3
  candidateArtifactEnvironment,
4
4
  type CandidateArtifactRoots,
@@ -6,9 +6,15 @@ import {
6
6
  import { mkdir, stat } from "node:fs/promises";
7
7
  import { delimiter, join } from "node:path";
8
8
  import { prepareIsolatedCommand } from "../hosts/isolation";
9
- import type { RuntimePolicy } from "../runtime-config";
10
- import { prepareRuntimeState } from "../runtime-state";
9
+ import type { RuntimePolicy } from "../runtime/config";
10
+ import { prepareRuntimeState } from "../runtime/state";
11
+ import { sharedCacheDenyRoots } from "../runtime/shared-cache";
11
12
  import { hashJson } from "../identity";
13
+ import {
14
+ captureBoundedText,
15
+ ProcessDeadline,
16
+ terminateOwnedProcessGroup,
17
+ } from "../process-lifecycle";
12
18
 
13
19
  const DEFAULT_TIMEOUT_MS = 30_000;
14
20
  const MAX_TIMEOUT_MS = 120_000;
@@ -239,32 +245,12 @@ function forbiddenPatternFailure(
239
245
  async function boundedOutput(
240
246
  stream: ReadableStream<Uint8Array>,
241
247
  ): Promise<string> {
242
- const chunks: Uint8Array[] = [];
243
- let size = 0;
244
- for await (const chunk of stream) {
245
- size += chunk.byteLength;
246
- if (size > MAX_OUTPUT_BYTES) throw new Error("shell stdout exceeds 1 MiB");
247
- chunks.push(chunk);
248
- }
249
- return new TextDecoder("utf-8", { fatal: true }).decode(
250
- Buffer.concat(chunks),
251
- );
252
- }
253
-
254
- function stopProcess(proc: Bun.Subprocess): void {
255
- if (process.platform !== "win32") {
256
- try {
257
- process.kill(-proc.pid, "SIGKILL");
258
- return;
259
- } catch {
260
- // The group may already be gone.
261
- }
262
- }
263
- try {
264
- proc.kill("SIGKILL");
265
- } catch {
266
- // The process already exited.
267
- }
248
+ return captureBoundedText(stream, {
249
+ limit: MAX_OUTPUT_BYTES,
250
+ overflow: "reject",
251
+ exceeded: () => new Error("shell stdout exceeds 1 MiB"),
252
+ cancelOnError: true,
253
+ });
268
254
  }
269
255
 
270
256
  interface ShellRunOptions {
@@ -294,7 +280,11 @@ async function shellRuntime(options: ShellRunOptions) {
294
280
  privateRuntimeRoot: runtime.root,
295
281
  };
296
282
  }
297
- const root = join(options.workspace, ".git", "sevro-runtime"),
283
+ const root = join(
284
+ options.privateStateRoot,
285
+ "runtime",
286
+ hashJson(options.workspace),
287
+ ),
298
288
  home = join(root, "check-home"),
299
289
  temp = join(root, "check-tmp");
300
290
  await Promise.all([
@@ -305,9 +295,10 @@ async function shellRuntime(options: ShellRunOptions) {
305
295
  root,
306
296
  home,
307
297
  temp,
308
- uvCache: join(root, "uv-cache"),
298
+ uvCache: join(options.workspace, ".git", "sevro-runtime", "uv-cache"),
309
299
  environment: {} as Record<string, string>,
310
- privateRuntimeRoot: undefined,
300
+ privateRuntimeRoot: root,
301
+ writableCacheRoots: [],
311
302
  };
312
303
  }
313
304
  type ShellRuntime = Awaited<ReturnType<typeof shellRuntime>>;
@@ -368,30 +359,39 @@ function shellStdout(
368
359
  }
369
360
  class ShellExecution {
370
361
  private proc?: Bun.Subprocess;
371
- private timer?: ReturnType<typeof setTimeout>;
372
- private cancel?: () => void;
362
+ private readonly deadline: ProcessDeadline;
373
363
  constructor(
374
364
  private check: PreparedShellCheck,
375
365
  private options: ShellRunOptions,
376
366
  private isolation: Isolation,
377
367
  private runtime: ShellRuntime,
378
- ) {}
368
+ ) {
369
+ this.deadline = new ProcessDeadline({
370
+ timeoutMs: check.timeoutMs,
371
+ signal: options.signal,
372
+ interruption: (reason) =>
373
+ new Error(
374
+ reason === "timeout"
375
+ ? "shell check timed out"
376
+ : "shell check cancelled",
377
+ ),
378
+ });
379
+ }
379
380
  async run(): Promise<ShellCheckResult> {
380
381
  try {
381
382
  const running = this.spawn();
382
383
  const stream = shellStdout(this.check, running.stdout);
383
- const timeout = this.deadline();
384
- const aborted = this.cancellation();
385
- const completed = Promise.all([
386
- running.exited,
387
- stream ? boundedOutput(stream) : Promise.resolve(null),
388
- ]).then(([exitCode, stdout]) => ({ exitCode, stdout }));
389
- return await Promise.race([completed, timeout, aborted]);
384
+ return await this.deadline.wait(() =>
385
+ Promise.all([
386
+ running.exited,
387
+ stream ? boundedOutput(stream) : Promise.resolve(null),
388
+ ]).then(([exitCode, stdout]) => ({ exitCode, stdout })),
389
+ );
390
390
  } catch (error) {
391
391
  await this.stop();
392
392
  throw error;
393
393
  } finally {
394
- this.releaseListeners();
394
+ this.deadline.dispose();
395
395
  await this.isolation.release();
396
396
  }
397
397
  }
@@ -406,35 +406,12 @@ class ShellExecution {
406
406
  });
407
407
  return this.proc;
408
408
  }
409
- private deadline(): Promise<never> {
410
- return new Promise((_resolve, reject) => {
411
- this.timer = setTimeout(() => {
412
- reject(new Error("shell check timed out"));
413
- }, this.check.timeoutMs);
414
- });
415
- }
416
- private cancellation(): Promise<never> {
417
- return new Promise((_resolve, reject) => {
418
- const signal = this.options.signal;
419
- if (!signal) return;
420
- this.cancel = () => {
421
- reject(new Error("shell check cancelled"));
422
- };
423
- if (signal.aborted) this.cancel();
424
- else signal.addEventListener("abort", this.cancel, { once: true });
425
- });
426
- }
427
409
  private async stop(): Promise<void> {
428
410
  const proc = this.proc;
429
411
  if (!proc) return;
430
- stopProcess(proc);
412
+ terminateOwnedProcessGroup(proc);
431
413
  await proc.exited;
432
414
  }
433
- private releaseListeners(): void {
434
- if (this.timer) clearTimeout(this.timer);
435
- if (this.cancel)
436
- this.options.signal?.removeEventListener("abort", this.cancel);
437
- }
438
415
  }
439
416
 
440
417
  /** Run a check without inherited credentials or retaining raw process output. */
@@ -451,17 +428,22 @@ export async function runShellCheck(
451
428
  protectedRootsCanonical: options.protectedRootsCanonical,
452
429
  privateStateRoot: options.privateStateRoot,
453
430
  denyNetwork: true,
431
+ localHttp: runtimeLocalHttpEnabled(options.runtimePolicy, "checks"),
454
432
  readOnlyRoots: options.runtimePolicy?.readOnlyRoots,
455
433
  candidateArtifactRoots: options.candidateArtifactRoots,
456
434
  transcriptReadRoots: options.candidateTranscriptRoot
457
435
  ? [options.candidateTranscriptRoot]
458
436
  : [],
459
437
  writableRuntimeRoot: runtime.privateRuntimeRoot,
438
+ writableCacheRoots: runtime.writableCacheRoots,
460
439
  });
461
440
  await requireShellRuntimeCache(options, runtime);
462
441
  return new ShellExecution(check, options, isolated, runtime).run();
463
442
  }
464
443
 
465
444
  function shellSeedSources(options: ShellRunOptions): string[] {
466
- return runtimeSeedSources(options.runtimePolicy);
445
+ return [
446
+ ...runtimeSeedSources(options.runtimePolicy),
447
+ ...sharedCacheDenyRoots(options.runtimePolicy, "checks"),
448
+ ];
467
449
  }
@@ -0,0 +1,52 @@
1
+ import { readFile, stat } from "node:fs/promises";
2
+ import { isRecord } from "./value-guards";
3
+
4
+ function credentialStrings(value: unknown): string[] {
5
+ const pending: unknown[] = [value];
6
+ const secrets: string[] = [];
7
+ for (let visited = 0; visited < 256; visited++) {
8
+ const item = pending.pop();
9
+ if (typeof item === "string") {
10
+ if (item.length >= 8) secrets.push(item);
11
+ } else if (isRecord(item)) pending.push(...Object.values(item));
12
+ }
13
+ return secrets;
14
+ }
15
+
16
+ /** Read credentials only for scrubbing; failures belong to the actual host preparation. */
17
+ export async function diagnosticSecrets(path: string): Promise<string[]> {
18
+ try {
19
+ if ((await stat(path)).size > 1024 * 1024) return [];
20
+ const text = await readFile(path, "utf8");
21
+ try {
22
+ const value: unknown = JSON.parse(text);
23
+ return credentialStrings(value);
24
+ } catch {
25
+ return text.trim() ? [text.trim()] : [];
26
+ }
27
+ } catch {
28
+ return [];
29
+ }
30
+ }
31
+
32
+ export function redactDiagnosticText(text: string, secrets: string[]): string {
33
+ for (const secret of secrets) {
34
+ text = text.replaceAll(secret, "[redacted]");
35
+ text = redactCredentialSuffix(text, secret);
36
+ }
37
+ return text
38
+ .replace(/(authorization\s*:\s*)[^\r\n]+/gi, "$1[redacted]")
39
+ .replace(/\bBearer\s+\S+/gi, "Bearer [redacted]")
40
+ .replace(/\bsk-[A-Za-z0-9_-]+/g, "[redacted]")
41
+ .replace(
42
+ /((?:api[_-]?key|access[_-]?token|refresh[_-]?token)\s*[=:]\s*)\S+/gi,
43
+ "$1[redacted]",
44
+ );
45
+ }
46
+
47
+ function redactCredentialSuffix(text: string, secret: string): string {
48
+ const start = text.lastIndexOf(secret.slice(0, Math.min(8, secret.length)));
49
+ if (start >= 0 && secret.startsWith(text.slice(start)))
50
+ return text.slice(0, start) + "[redacted]";
51
+ return text;
52
+ }
@@ -0,0 +1,331 @@
1
+ import type { HostResult } from "./evaluation/types";
2
+ import type { RuntimeRole } from "./runtime/state";
3
+ import {
4
+ diagnosticSecrets,
5
+ redactDiagnosticText,
6
+ } from "./host-diagnostic-redaction";
7
+
8
+ const MAX_TEXT_BYTES = 64 * 1024;
9
+ interface ProcessDiagnostic {
10
+ code: number | null;
11
+ out: string;
12
+ stderr: Uint8Array;
13
+ stderrTruncated: boolean;
14
+ signal: string | null;
15
+ durationMs: number;
16
+ stdoutTruncated?: boolean;
17
+ reason: "exit" | "timeout" | "cancelled" | "error" | "spawn";
18
+ }
19
+
20
+ export type ProcessFailureReason = ProcessDiagnostic["reason"];
21
+
22
+ export class HostDiagnosticPersistenceError extends Error {
23
+ constructor(persistenceCause: unknown, hostCause: unknown) {
24
+ super("host diagnostic persistence failed", {
25
+ cause: new AggregateError(
26
+ [hostCause, persistenceCause],
27
+ "host and persistence failures",
28
+ ),
29
+ });
30
+ }
31
+ }
32
+
33
+ export function rethrowDiagnosticPersistenceError(cause: unknown): void {
34
+ if (cause instanceof HostDiagnosticPersistenceError) throw cause;
35
+ }
36
+
37
+ /** Drain observers capture only a prefix; completion records actual native termination. */
38
+ export class HostProcessDiagnostics {
39
+ readonly stdout = new DiagnosticStream();
40
+ readonly stderr = new DiagnosticStream();
41
+ private started = performance.now();
42
+ constructor(private diagnostics: HostDiagnostics) {}
43
+ recordUnstarted(): void {
44
+ if (!this.diagnostics.enabled) return;
45
+ this.diagnostics.process({
46
+ code: null,
47
+ signal: null,
48
+ durationMs: performance.now() - this.started,
49
+ out: "",
50
+ stderr: new Uint8Array(),
51
+ stderrTruncated: false,
52
+ reason: "spawn",
53
+ });
54
+ }
55
+ async record(
56
+ process: { exited: Promise<number | null>; signalCode?: string | null },
57
+ reason: ProcessFailureReason = "exit",
58
+ ): Promise<void> {
59
+ if (!this.diagnostics.enabled) return;
60
+ this.diagnostics.process({
61
+ code: await process.exited,
62
+ signal: process.signalCode ?? null,
63
+ durationMs: performance.now() - this.started,
64
+ out: this.stdout.bytes().toString("utf8"),
65
+ stdoutTruncated: this.stdout.truncated,
66
+ stderr: this.stderr.bytes(),
67
+ stderrTruncated: this.stderr.truncated,
68
+ reason,
69
+ });
70
+ }
71
+ }
72
+
73
+ function boundedText(bytes: Uint8Array, secrets: string[]) {
74
+ const redacted = Buffer.from(
75
+ redactDiagnosticText(
76
+ Buffer.from(bytes.subarray(0, MAX_TEXT_BYTES)).toString("utf8"),
77
+ secrets,
78
+ ),
79
+ );
80
+ return {
81
+ text: new TextDecoder().decode(redacted.subarray(0, MAX_TEXT_BYTES), {
82
+ stream: true,
83
+ }),
84
+ truncated:
85
+ bytes.byteLength > MAX_TEXT_BYTES || redacted.byteLength > MAX_TEXT_BYTES,
86
+ };
87
+ }
88
+
89
+ /** Keep a byte-bounded prefix while the owning transport continues draining. */
90
+ export class DiagnosticStream {
91
+ private chunks: Uint8Array[] = [];
92
+ private size = 0;
93
+ truncated = false;
94
+ add(bytes: Uint8Array): void {
95
+ const retained = bytes.subarray(0, Math.max(0, MAX_TEXT_BYTES - this.size));
96
+ if (retained.byteLength) this.chunks.push(Buffer.from(retained));
97
+ this.size += retained.byteLength;
98
+ this.truncated ||= retained.byteLength !== bytes.byteLength;
99
+ }
100
+ bytes(): Buffer {
101
+ return Buffer.concat(this.chunks);
102
+ }
103
+ }
104
+
105
+ function errorCode(cause: Error): { code?: string | number } {
106
+ if (!("code" in cause)) return {};
107
+ const code: unknown = cause.code;
108
+ return typeof code === "string" || typeof code === "number" ? { code } : {};
109
+ }
110
+
111
+ function errorEvidence(
112
+ cause: unknown,
113
+ secrets: string[],
114
+ ): Record<string, unknown> {
115
+ return errorChain(cause, secrets, new Set());
116
+ }
117
+
118
+ function errorChain(
119
+ cause: unknown,
120
+ secrets: string[],
121
+ seen: Set<Error>,
122
+ ): Record<string, unknown> {
123
+ if (!(cause instanceof Error))
124
+ return {
125
+ name: "NonError",
126
+ message: redactDiagnosticText(String(cause).slice(0, 8192), secrets),
127
+ };
128
+ if (seen.has(cause) || seen.size === 8) return { truncated: true };
129
+ seen.add(cause);
130
+ return {
131
+ ...errorDetails(cause, secrets),
132
+ ...(cause.cause === undefined
133
+ ? {}
134
+ : { cause: errorChain(cause.cause, secrets, seen) }),
135
+ };
136
+ }
137
+
138
+ function errorDetails(cause: Error, secrets: string[]) {
139
+ return {
140
+ name: redactDiagnosticText(cause.name.slice(0, 200), secrets),
141
+ message: redactDiagnosticText(cause.message.slice(0, 8192), secrets),
142
+ stack: redactDiagnosticText(cause.stack?.slice(0, 8192) ?? "", secrets),
143
+ ...errorCode(cause),
144
+ };
145
+ }
146
+
147
+ /** A cleanup exception must not replace an already captured host/persistence failure. */
148
+ export async function cleanupNativeState(
149
+ diagnostics: HostDiagnostics,
150
+ cleanup: () => Promise<void>,
151
+ ): Promise<void> {
152
+ beginNativeCleanup(diagnostics);
153
+ try {
154
+ await cleanup();
155
+ } catch (cause) {
156
+ if (!diagnostics.failureCaptured) throw cause;
157
+ console.warn(
158
+ "native state cleanup failed; original host failure preserved",
159
+ );
160
+ }
161
+ }
162
+
163
+ function beginNativeCleanup(diagnostics: HostDiagnostics): void {
164
+ if (!diagnostics.failureCaptured) diagnostics.stage = "cleanup";
165
+ }
166
+
167
+ export function diagnosticConfiguration(enabled: boolean | undefined) {
168
+ return enabled ? { "sevro.host.retain-diagnostics": true } : {};
169
+ }
170
+
171
+ export function createHostDiagnostics(
172
+ host: "codex" | "claude",
173
+ enabled: boolean | undefined,
174
+ request: {
175
+ runtimeRole?: RuntimeRole;
176
+ retainHostDiagnostics?: (bytes: Uint8Array) => Promise<void>;
177
+ },
178
+ ): HostDiagnostics {
179
+ return enabled
180
+ ? new RetainedHostDiagnostics(
181
+ host,
182
+ request.runtimeRole,
183
+ request.retainHostDiagnostics,
184
+ )
185
+ : new DisabledHostDiagnostics();
186
+ }
187
+
188
+ export async function diagnoseHostRequest(
189
+ diagnostics: HostDiagnostics,
190
+ execute: () => Promise<HostResult>,
191
+ ): Promise<HostResult> {
192
+ try {
193
+ return await execute();
194
+ } catch (cause) {
195
+ if (cause instanceof HostDiagnosticPersistenceError) throw cause;
196
+ if (!diagnostics.enabled) throw cause;
197
+ const result: HostResult = {
198
+ finalMessage: null,
199
+ complete: false,
200
+ executionFailed: true,
201
+ actualCondition: "passive",
202
+ };
203
+ await diagnostics.retain(result, cause);
204
+ return result;
205
+ }
206
+ }
207
+
208
+ export interface HostDiagnostics {
209
+ readonly enabled: boolean;
210
+ stage: string;
211
+ failure: unknown;
212
+ readonly failureCaptured: boolean;
213
+ readCredentials(path: string | undefined): Promise<void>;
214
+ registerCredentials(
215
+ environment: Record<string, string>,
216
+ names: readonly string[],
217
+ ): void;
218
+ process(result: ProcessDiagnostic): void;
219
+ retain(result: HostResult, cause: unknown): Promise<void>;
220
+ retainFailure(result: HostResult, message: string): Promise<void>;
221
+ }
222
+
223
+ class DisabledHostDiagnostics implements HostDiagnostics {
224
+ readonly enabled = false;
225
+ stage = "preparation";
226
+ failure: unknown;
227
+ readonly failureCaptured = false;
228
+
229
+ readCredentials(): Promise<void> {
230
+ return Promise.resolve();
231
+ }
232
+
233
+ registerCredentials(): void {}
234
+
235
+ process(): void {}
236
+
237
+ retain(): Promise<void> {
238
+ return Promise.resolve();
239
+ }
240
+
241
+ retainFailure(): Promise<void> {
242
+ return Promise.resolve();
243
+ }
244
+ }
245
+
246
+ /** Private failure evidence, independent of native sessions and transcript access. */
247
+ class RetainedHostDiagnostics implements HostDiagnostics {
248
+ readonly enabled = true;
249
+ stage = "preparation";
250
+ failure: unknown;
251
+ failureCaptured = false;
252
+ private processes: Record<string, unknown>[] = [];
253
+ private processesDropped = 0;
254
+ private secrets: string[] = [];
255
+ constructor(
256
+ private host: "codex" | "claude",
257
+ private role: RuntimeRole = "candidate",
258
+ private persist?: (bytes: Uint8Array) => Promise<void>,
259
+ ) {}
260
+
261
+ async readCredentials(path: string | undefined): Promise<void> {
262
+ if (path) this.secrets.push(...(await diagnosticSecrets(path)));
263
+ }
264
+
265
+ registerCredentials(
266
+ environment: Record<string, string>,
267
+ names: readonly string[],
268
+ ): void {
269
+ for (const name of names) {
270
+ const value = environment[name];
271
+ if (value) this.secrets.push(value);
272
+ }
273
+ }
274
+
275
+ process(result: ProcessDiagnostic): void {
276
+ const stdout = boundedText(Buffer.from(result.out), this.secrets);
277
+ const stderr = boundedText(result.stderr, this.secrets);
278
+ this.addProcess({
279
+ stage: this.stage,
280
+ code: result.code,
281
+ signal: result.signal,
282
+ durationMs: result.durationMs,
283
+ reason: result.reason,
284
+ stdout: stdout.text,
285
+ stdoutTruncated: stdout.truncated || result.stdoutTruncated === true,
286
+ stderr: stderr.text,
287
+ stderrTruncated: stderr.truncated || result.stderrTruncated,
288
+ });
289
+ }
290
+
291
+ private addProcess(receipt: Record<string, unknown>): void {
292
+ this.processes.push(receipt);
293
+ while (
294
+ this.processes.length > 32 ||
295
+ Buffer.byteLength(JSON.stringify(this.processes)) > 2 * 1024 * 1024
296
+ ) {
297
+ this.processes.shift();
298
+ this.processesDropped++;
299
+ }
300
+ }
301
+
302
+ async retainFailure(result: HostResult, message: string): Promise<void> {
303
+ if (result.executionFailed)
304
+ await this.retain(result, this.failure ?? new Error(message));
305
+ }
306
+
307
+ async retain(result: HostResult, cause: unknown): Promise<void> {
308
+ this.failureCaptured = true;
309
+ const bytes = Buffer.from(
310
+ JSON.stringify({
311
+ format: "sevro.host-diagnostics.v1",
312
+ host: this.host,
313
+ role: this.role,
314
+ stage: this.stage,
315
+ error: errorEvidence(cause, this.secrets),
316
+ processes: this.processes,
317
+ processesDropped: this.processesDropped,
318
+ }),
319
+ );
320
+ if (this.persist) {
321
+ try {
322
+ await this.persist(bytes);
323
+ } catch (persistenceCause) {
324
+ throw new HostDiagnosticPersistenceError(persistenceCause, cause);
325
+ }
326
+ return;
327
+ }
328
+ result.artifacts ??= [];
329
+ result.artifacts.push({ id: "sevro.host.diagnostics", bytes });
330
+ }
331
+ }