crysk 0.6.1 → 0.8.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 (179) hide show
  1. package/README.md +28 -22
  2. package/dist/SKILL-1q3658n0.md +74 -0
  3. package/dist/SKILL-25j4q9b8.md +143 -0
  4. package/dist/SKILL-32cmme3j.md +143 -0
  5. package/dist/SKILL-4747bhne.md +143 -0
  6. package/dist/SKILL-4jc3sv23.md +74 -0
  7. package/dist/SKILL-c0492rch.md +540 -0
  8. package/dist/SKILL-d1nzkhhr.md +74 -0
  9. package/dist/SKILL-f8p7kvx0.md +143 -0
  10. package/dist/SKILL-gxd45g38.md +553 -0
  11. package/dist/SKILL-rec41991.md +66 -0
  12. package/dist/SKILL-zze8zj5g.md +536 -0
  13. package/dist/adjudicator-eckqh14e.md +78 -0
  14. package/dist/ci-admission-qn797a6c.md +128 -0
  15. package/dist/cli.js +18608 -18523
  16. package/dist/consolidate-4xw1pkg7.md +24 -0
  17. package/dist/deep-review-logic-dgr112y1.md +196 -0
  18. package/dist/deep-review-logic-fck5z4xe.md +197 -0
  19. package/dist/deep-review-logic-h1fpamfh.md +192 -0
  20. package/dist/deep-review-logic-m4vk5683.md +196 -0
  21. package/dist/deep-review-logic-z63kmgmc.md +157 -0
  22. package/dist/deep-review-parity-7x7s5d81.md +148 -0
  23. package/dist/deep-review-parity-9wxmynsc.md +115 -0
  24. package/dist/deep-review-parity-bwwe7cx6.md +152 -0
  25. package/dist/deep-review-parity-e50pw3pk.md +117 -0
  26. package/dist/deep-review-parity-ennmm9m1.md +151 -0
  27. package/{prompts/default/deep-review-parity.md → dist/deep-review-parity-h7y6y2he.md} +14 -16
  28. package/dist/deep-review-parity-xg6wy69c.md +151 -0
  29. package/dist/deep-review-reliability-6h3c4gmw.md +125 -0
  30. package/dist/deep-review-reliability-dcjab69x.md +122 -0
  31. package/dist/deep-review-reliability-ec7b5pbk.md +159 -0
  32. package/dist/deep-review-reliability-hvasvmt9.md +153 -0
  33. package/dist/deep-review-reliability-mjnayxmt.md +158 -0
  34. package/dist/deep-review-reliability-mm7fnayf.md +124 -0
  35. package/dist/deep-review-reliability-xk4xvc2w.md +158 -0
  36. package/dist/deep-review-resilience-0esd5qeh.md +148 -0
  37. package/dist/deep-review-resilience-envgcmpy.md +115 -0
  38. package/dist/deep-review-resilience-fgx8ythf.md +151 -0
  39. package/dist/deep-review-resilience-mq5qa6ad.md +152 -0
  40. package/dist/deep-review-resilience-pwh1gawp.md +151 -0
  41. package/dist/deep-review-resilience-s9n21dkx.md +117 -0
  42. package/dist/deep-review-resilience-vz4qskea.md +119 -0
  43. package/dist/deep-review-security-1vwjdc9q.md +205 -0
  44. package/dist/deep-review-security-x1cc1z00.md +205 -0
  45. package/dist/index.js +590 -485
  46. package/dist/opencode-ci-jc8mf4wn.md +106 -0
  47. package/dist/opencode-ci-qrcv7a99.md +106 -0
  48. package/dist/opencode-ci-rdpcc7xc.md +106 -0
  49. package/dist/opencode-ci-yt0k74t0.md +106 -0
  50. package/dist/review-refuter-7qffwtzz.md +77 -0
  51. package/dist/verifier-tyh8r45v.md +40 -0
  52. package/dist/verifier-xf4xj701.md +40 -0
  53. package/dist/workflow-andv2dvm.yml +158 -0
  54. package/package.json +1 -1
  55. package/prompts/consolidate.md +1 -1
  56. package/prompts/default/PROVENANCE.md +6 -0
  57. package/prompts/default/deep-review-logic.md +56 -15
  58. package/prompts/default/deep-review-reliability.md +64 -30
  59. package/prompts/default/deep-review-resilience.md +71 -37
  60. package/prompts/default/deep-review-security.md +205 -0
  61. package/prompts/default/review-refuter.md +1 -1
  62. package/prompts/verifier.md +40 -0
  63. package/skills/crysk/SKILL.md +6 -6
  64. package/skills/crysk-ci-setup/SKILL.md +32 -33
  65. package/skills/crysk-ci-setup/assets/workflow.yml +5 -10
  66. package/skills/crysk-ci-setup/references/ci-admission.md +5 -5
  67. package/skills/crysk-ci-setup/references/opencode-ci.md +6 -6
  68. package/skills/crysk-loop/SKILL.md +37 -54
  69. package/skills/crysk-loop/adjudicator.md +10 -18
  70. package/skills/crysk-pr/SKILL.md +1 -1
  71. package/src/asset-manifest.ts +6 -6
  72. package/src/assets.ts +4 -4
  73. package/src/brand.ts +20 -30
  74. package/src/ci/gates.ts +155 -192
  75. package/src/ci/outcome.ts +4 -31
  76. package/src/ci/platform-limits.ts +135 -0
  77. package/src/ci/platform-report.ts +212 -126
  78. package/src/ci/platform.ts +28 -26
  79. package/src/ci/reporter.ts +25 -26
  80. package/src/ci/review-admission.ts +1 -2
  81. package/src/ci/review-levels.ts +12 -53
  82. package/src/ci/setup.ts +96 -66
  83. package/src/ci/trace-bundle.ts +11 -27
  84. package/src/ci/trace-transcripts.ts +4 -6
  85. package/src/ci/trigger.ts +3 -5
  86. package/src/cli.ts +6 -5
  87. package/src/commands/ci-setup-questions.ts +2 -55
  88. package/src/commands/ci-setup.ts +2 -26
  89. package/src/commands/triage.ts +131 -38
  90. package/src/commands/usage.ts +0 -1
  91. package/src/compare/compare.ts +3 -3
  92. package/src/compare/floor-test.ts +30 -201
  93. package/src/compare/hunter-exam.ts +631 -0
  94. package/src/config/config.ts +6 -30
  95. package/src/diversity/accounting.ts +0 -26
  96. package/src/diversity/benchmark.ts +1 -17
  97. package/src/diversity/identity.ts +0 -14
  98. package/src/diversity/pipeline-integration.ts +0 -29
  99. package/src/doctor.ts +3 -6
  100. package/src/execution/claude-session-trace.ts +7 -2
  101. package/src/execution/contracts.ts +15 -9
  102. package/src/execution/harness.ts +165 -187
  103. package/src/execution/spend-limiter.ts +39 -131
  104. package/src/execution/usage-facts.ts +397 -0
  105. package/src/execution/usage-normalized.ts +33 -86
  106. package/src/harness/materiality-judge.ts +0 -5
  107. package/src/harness/pipeline-plan.ts +0 -3
  108. package/src/harness/replay-rows.ts +0 -5
  109. package/src/harness/route-runner.ts +2 -2
  110. package/src/harness/single-step.ts +3 -9
  111. package/src/index.ts +1 -1
  112. package/src/mcp/preflight.ts +2 -2
  113. package/src/model/catalog.ts +1 -3
  114. package/src/model/free-discovery.ts +11 -78
  115. package/src/model/model-name.ts +16 -5
  116. package/src/model/profiles/claude.ts +1 -2
  117. package/src/model/profiles/openai.ts +1 -2
  118. package/src/model/profiles.ts +2 -3
  119. package/src/model/provider-capabilities.ts +16 -55
  120. package/src/model/routing.ts +39 -36
  121. package/src/pr/admission.ts +6 -6
  122. package/src/pr/answered-findings.ts +8 -4
  123. package/src/pr/ci-publish.ts +0 -2
  124. package/src/pr/discovery.ts +4 -25
  125. package/src/pr/plan.ts +40 -121
  126. package/src/pr/pr-status.ts +14 -2
  127. package/src/pr/pr.ts +1 -1
  128. package/src/pr/preflight.ts +44 -15
  129. package/src/pr/publish-outcome.ts +1 -19
  130. package/src/pr/review-pr.ts +15 -47
  131. package/src/pr/target.ts +76 -82
  132. package/src/production-runtime.ts +52 -114
  133. package/src/progress.ts +0 -54
  134. package/src/rereview/prepare.ts +4 -1
  135. package/src/rereview/state.ts +1 -1
  136. package/src/rereview/verify.ts +15 -9
  137. package/src/review/boundary.ts +5 -8
  138. package/src/review/drafts.ts +3 -3
  139. package/src/review/findings-conformance.ts +1 -1
  140. package/src/review/findings.ts +0 -4
  141. package/src/review/pipeline.ts +194 -407
  142. package/src/review/preflight.ts +175 -201
  143. package/src/review/prompt-set.ts +1 -2
  144. package/src/review/report.ts +2 -162
  145. package/src/review/review.ts +25 -74
  146. package/src/review/route-preflight.ts +19 -35
  147. package/src/review/run.ts +37 -73
  148. package/src/review/size-gate.ts +3 -3
  149. package/src/review/spec.ts +6 -11
  150. package/src/review/step-runner.ts +4 -4
  151. package/src/runner-authority.ts +10 -9
  152. package/src/security/credential-broker.ts +5 -6
  153. package/src/server/preflight.ts +0 -1
  154. package/src/server/server.ts +17 -6
  155. package/src/store/activity.ts +1 -2
  156. package/src/store/backfill-preflight.ts +0 -3
  157. package/src/store/backfill.ts +0 -1
  158. package/src/store/metrics-preflight.ts +4 -14
  159. package/src/store/metrics.ts +5 -5
  160. package/src/store/preflight.ts +0 -13
  161. package/src/store/store.ts +11 -12
  162. package/src/transport-registry.ts +6 -30
  163. package/src/transports/claude-code-cli.ts +149 -215
  164. package/src/transports/opencode-client.ts +4 -24
  165. package/src/transports/opencode-sdk.ts +17 -112
  166. package/src/triage/plain-reply.ts +37 -0
  167. package/src/ui/activity.ts +1 -6
  168. package/src/ui/config-edit.ts +14 -67
  169. package/src/ui/config.ts +3 -8
  170. package/src/ui/plan.ts +11 -198
  171. package/src/ui/progress.ts +3 -49
  172. package/src/ui/result.ts +18 -77
  173. package/src/ui/review-menu.ts +3 -12
  174. package/src/ui/select.ts +9 -79
  175. package/src/updater.ts +1 -1
  176. package/src/usage.ts +1 -7
  177. package/prompts/scout.md +0 -140
  178. package/src/ci/platform-estimate.ts +0 -111
  179. package/src/review/scout.ts +0 -323
@@ -1,5 +1,7 @@
1
+ import { createReadStream, existsSync } from "node:fs";
1
2
  import { mkdir } from "node:fs/promises";
2
3
  import path from "node:path";
4
+ import { createInterface } from "node:readline";
3
5
  import { releaseExecutionSnapshot } from "#model/exec-snapshots";
4
6
  import {
5
7
  type ExecutableAllowlistEntry,
@@ -53,6 +55,7 @@ import {
53
55
  type AuthEvent,
54
56
  isTransportFailureCause,
55
57
  type ProviderTransport,
58
+ type ResolvedModelRoute,
56
59
  type StepAdmissionGate,
57
60
  type TransportFailureCause,
58
61
  type TransportOutcome,
@@ -81,7 +84,6 @@ import {
81
84
  beginStep,
82
85
  nextCycle,
83
86
  type ReserveToken,
84
- type SettlementDecision,
85
87
  type SpendLedger,
86
88
  type SpendReservation,
87
89
  SpendReservationFencedError,
@@ -92,12 +94,20 @@ import {
92
94
  FORMAT_RETRY_REMINDER,
93
95
  settlementReceiptPath,
94
96
  } from "./step-artifacts";
97
+ import {
98
+ type AttemptUsageFacts,
99
+ claudeAttemptUsageFacts,
100
+ credentialOfBillingMode,
101
+ openCodeAttemptUsageFacts,
102
+ usageFactsPath,
103
+ } from "./usage-facts";
95
104
  import {
96
105
  envBillsMetered,
97
106
  type NormalizedUsage,
98
107
  normalizeUnavailableUsage,
99
108
  projectLegacyUsage,
100
109
  sumNormalizedUsage,
110
+ type UsageBillingMode,
101
111
  } from "./usage-normalized";
102
112
 
103
113
  // §7 line 416: injected sleep for the `rate_limit` capped-exponential
@@ -249,13 +259,6 @@ export interface StepExecutionHarnessOptions {
249
259
  // every existing caller (PR5a's own tests included) keeps its ledger-free
250
260
  // shape unmodified.
251
261
  readonly spendLedger?: SpendLedger;
252
- // What each attempt reserves BEFORE its actual cost is known. Estimating a
253
- // real per-step dollar figure from prompt/model/provider pricing is a
254
- // caller concern this slice does not attempt (same framing as PR5a's own
255
- // `rateLimitBucketId` default: deriving the real number belongs to
256
- // whoever composes the run, not to this harness). Defaults to 0 so an
257
- // unconfigured caller reserves nothing rather than an invented ceiling.
258
- readonly reservedUsdPerAttempt?: number;
259
262
  // #279: the BINDING's own reserve decision — true for a metered or
260
263
  // free-credential route (production-runtime.ts's `reservesSpend`),
261
264
  // independent of whether `spendLedger` is configured at all. Defaults to
@@ -328,7 +331,7 @@ const ENV_PASSTHROUGH = [
328
331
  // disagreeing about the same variable. pr-hero's first real CI self-review
329
332
  // (2026-08-27) passed the gate, passed the size gate, then lost all three
330
333
  // hunters and the summarizer in three seconds to "Not logged in · Please run
331
- // /login", $0.00 spent, because this projection dropped the token the action
334
+ // /login", because this projection dropped the token the action
332
335
  // had just handed it. It is the same credential class as ANTHROPIC_API_KEY
333
336
  // directly above, and it is the ONLY route on Linux: credential projection
334
337
  // needs darwin + /usr/bin/security, so every CI runner falls back to exactly
@@ -347,9 +350,9 @@ const ENV_PASSTHROUGH = [
347
350
  // untouched on exactly the path whose purpose is isolating the credential.
348
351
  //
349
352
  // The leak was inert while nothing read it back. #177 ended that: the Claude
350
- // CLI transport now derives `billingMode`/`costSource` from this very env
351
- // (`claudeCliCostBasis` → `envBillsMetered`), so a developer running on the
352
- // projected subscription credential got the review filed as metered cash on
353
+ // CLI transport now derives `billingMode` from this very env
354
+ // (`claudeCliBillingMode` → `envBillsMetered`), so a developer running on the
355
+ // projected subscription credential got the review filed as metered on
353
356
  // the strength of a key the projection was supposed to have replaced. Both
354
357
  // readings of the leak are defects — either the CLI honours the leaked key
355
358
  // and the isolation promise is false, or it prefers the projected OAuth and
@@ -413,7 +416,7 @@ function notifyRetry(step: StepSpec, info: RetryInfo): void {
413
416
  const DESTROY_FAILED_ANNOTATION =
414
417
  "[pr-hero] credential projection destroy failed";
415
418
  const AMBIENT_METERED_ANNOTATION =
416
- "[pr-hero] ambient credential bills metered — this attempt reserves against the spend ledger and fences its bucket if the cost cannot be confirmed";
419
+ "[pr-hero] ambient credential bills metered — this attempt reserves against the spend ledger and fences its bucket if its usage cannot be confirmed";
417
420
  // The class is variable in the shape even though only
418
421
  // missing_subscription_record reaches the append today: the shape is what
419
422
  // the harness writes, the class list is a policy that may grow.
@@ -438,20 +441,96 @@ function copyClaudeSessionTrace(
438
441
  ).catch((): ClaudeTraceMiss => "session trace write failed");
439
442
  }
440
443
 
441
- async function withClaudeSessionTrace(
442
- step: StepSpec,
443
- request: TransportRequest,
444
- attempt: number,
444
+ function withTraceMissAnnotation(
445
445
  outcome: TransportOutcome,
446
- ): Promise<TransportOutcome> {
447
- const reason = await copyClaudeSessionTrace(step, request, attempt);
448
- if (reason === undefined) return outcome;
446
+ miss: ClaudeTraceMiss | undefined,
447
+ ): TransportOutcome {
448
+ if (miss === undefined) return outcome;
449
449
  return {
450
450
  ...outcome,
451
- stderrTail: `${outcome.stderrTail}\n${claudeTraceCaptureAnnotation(reason)}`,
451
+ stderrTail: `${outcome.stderrTail}\n${claudeTraceCaptureAnnotation(miss)}`,
452
452
  };
453
453
  }
454
454
 
455
+ // platform-owned-cost T6: what one attempt measured per model request, for the
456
+ // platform's `usage_facts` (usage-facts.ts). Read here because the session copy
457
+ // and the CLI's per-model summary are only certain to exist now. Like the trace
458
+ // copy it is never the attempt's failure: a facts file that could not be built
459
+ // or written is absent, and the run's report names no requests for the attempt.
460
+ async function writeAttemptUsageFacts(input: {
461
+ step: StepSpec;
462
+ // The route the attempt was sent, for a provider and model the source does not name.
463
+ route: ResolvedModelRoute;
464
+ attempt: number;
465
+ transport: ProviderTransport;
466
+ // The trace copy's answer for this attempt (copyClaudeSessionTrace).
467
+ miss: ClaudeTraceMiss | undefined;
468
+ // Absent on a cancelled attempt, which has no outcome.
469
+ outcome: TransportOutcome | undefined;
470
+ billingMode: UsageBillingMode;
471
+ }): Promise<void> {
472
+ const { step, route, attempt, outcome } = input;
473
+ try {
474
+ const credential = credentialOfBillingMode(input.billingMode);
475
+ if (credential === undefined) return;
476
+ let facts: AttemptUsageFacts | undefined;
477
+ if (input.transport.backend === "claude-code") {
478
+ const trace = claudeSessionTracePath(step.outPath, step.name, attempt);
479
+ const copied = input.miss === undefined && existsSync(trace);
480
+ // No copy and no miss is an attempt that spawned no CLI: nothing ran.
481
+ if (
482
+ !copied &&
483
+ input.miss === undefined &&
484
+ outcome?.modelUsage === undefined
485
+ ) {
486
+ return;
487
+ }
488
+ const claude = (session: AsyncIterable<string> | undefined) =>
489
+ claudeAttemptUsageFacts({
490
+ attempt,
491
+ credential,
492
+ provider: route.provider,
493
+ session,
494
+ summary: outcome?.modelUsage,
495
+ });
496
+ try {
497
+ // A line at a time: the copy can be 64 MiB (see claude-session-trace.ts).
498
+ facts = await claude(
499
+ copied
500
+ ? createInterface({
501
+ input: createReadStream(trace, "utf8"),
502
+ crlfDelay: Number.POSITIVE_INFINITY,
503
+ })
504
+ : undefined,
505
+ );
506
+ } catch {
507
+ // The copy could not be read: report it as missing (a `no_session` gap), so
508
+ // the CLI's per-model summary still stands in for its requests.
509
+ facts = await claude(undefined);
510
+ }
511
+ } else if (
512
+ input.transport.backend === "opencode" &&
513
+ outcome !== undefined
514
+ ) {
515
+ facts = openCodeAttemptUsageFacts({
516
+ attempt,
517
+ credential,
518
+ route: { provider: route.provider, model: route.modelSnapshot },
519
+ capture: outcome.diagnosticEvidence,
520
+ totals: outcome.usage.tokens,
521
+ });
522
+ }
523
+ if (facts === undefined) return;
524
+ await writeJsonAtomically(
525
+ usageFactsPath(step.outPath, step.name, attempt),
526
+ facts,
527
+ { compact: true },
528
+ );
529
+ } catch {
530
+ // See above: the facts are absent, the attempt is unchanged.
531
+ }
532
+ }
533
+
455
534
  export function isHarnessStderrAnnotation(line: string): boolean {
456
535
  const trimmed = line.trim();
457
536
  return (
@@ -588,7 +667,6 @@ export class StepExecutionHarness implements StepRunner {
588
667
  private readonly attemptAdmissionGate?: AttemptAdmissionGate;
589
668
  private readonly rateLimitBucketId: string;
590
669
  private readonly spendLedger?: SpendLedger;
591
- private readonly reservedUsdPerAttempt: number;
592
670
  private readonly reservesSpend: boolean;
593
671
  private readonly degradedProjectionBucketId?: string;
594
672
  private readonly attemptAdmissionTimeoutMs: number;
@@ -621,7 +699,6 @@ export class StepExecutionHarness implements StepRunner {
621
699
  this.rateLimitBucketId =
622
700
  options.rateLimitBucketId ?? DEFAULT_RATE_LIMIT_BUCKET_ID;
623
701
  this.spendLedger = options.spendLedger;
624
- this.reservedUsdPerAttempt = options.reservedUsdPerAttempt ?? 0;
625
702
  // #279: default `true` — see the WHY on `StepExecutionHarnessOptions.
626
703
  // reservesSpend` for why an unset flag must keep every pre-existing
627
704
  // `spendLedger`-configuring caller reserving unconditionally.
@@ -1162,7 +1239,7 @@ export class StepExecutionHarness implements StepRunner {
1162
1239
  // - attempt vs attempt: every retry receives its own receipt, so a later
1163
1240
  // attempt never clobbers an earlier one's rejected-event / fence record.
1164
1241
  // - step vs step: every step of a run shares ONE steps/ directory
1165
- // (hunters, summarizer, refuters, scout all write beside each other),
1242
+ // (hunters, summarizer, refuters all write beside each other),
1166
1243
  // so a name keyed only on the attempt made each step overwrite the
1167
1244
  // previous step's receipt. That is not hypothetical — a four-step run
1168
1245
  // left four attempt logs and exactly one receipt on disk, and three
@@ -1430,14 +1507,7 @@ export class StepExecutionHarness implements StepRunner {
1430
1507
  finalText: "",
1431
1508
  usage: normalizeUnavailableUsage({
1432
1509
  wallMs: harnessWatchdogMs ?? 0,
1433
- // A Claude child bills on the credential its env holds, which the run chose; the transport's own
1434
- // answer reads the engine's startup env and could name a credential the run stripped.
1435
- billingMode:
1436
- transport.backend === "claude-code"
1437
- ? envBillsMetered(this.projectedEnv)
1438
- ? "metered"
1439
- : "subscription"
1440
- : transport.billingMode,
1510
+ billingMode: this.attemptBillingMode(transport),
1441
1511
  }),
1442
1512
  stderrTail: `Step timed out after ${harnessWatchdogMs}ms`,
1443
1513
  timedOut: true,
@@ -1677,7 +1747,7 @@ export class StepExecutionHarness implements StepRunner {
1677
1747
  // `projectLegacyUsage` at every return point below, never accumulated
1678
1748
  // independently — that is what keeps the two shapes unable to drift.
1679
1749
  // `undefined` until the first attempt settles: seeding it with a
1680
- // billingMode/costSource-less zero would force `sumNormalizedUsage` to
1750
+ // billingMode-less zero would force `sumNormalizedUsage` to
1681
1751
  // collapse the run's real billing mode to "unknown" on the very first sum.
1682
1752
  let totalUsageV2: NormalizedUsage | undefined;
1683
1753
  let attempts = 0;
@@ -1971,6 +2041,17 @@ export class StepExecutionHarness implements StepRunner {
1971
2041
  };
1972
2042
  }
1973
2043
 
2044
+ // The billing mode of an attempt that has no usage record of its own (a watchdog
2045
+ // timeout, a cancellation). A Claude child bills on the credential its env holds,
2046
+ // which the run chose; the transport's own answer reads the engine's startup env
2047
+ // and could name a credential the run stripped.
2048
+ private attemptBillingMode(transport: ProviderTransport): UsageBillingMode {
2049
+ if (transport.backend !== "claude-code") {
2050
+ return transport.billingMode ?? "unknown";
2051
+ }
2052
+ return envBillsMetered(this.projectedEnv) ? "metered" : "subscription";
2053
+ }
2054
+
1974
2055
  // D1-08 PR0: the ONE place a single attempt (transient OR format-retry —
1975
2056
  // `kind` only affects logging/request framing) executes, gets logged, and
1976
2057
  // has its failure resolved to a §7 cause. Replaces the two
@@ -2049,8 +2130,8 @@ export class StepExecutionHarness implements StepRunner {
2049
2130
  // a METERED binding of this SAME provider+credential would compute,
2050
2131
  // production-runtime.ts) — never this binding's own subscription
2051
2132
  // bucket. Fencing the subscription bucket would block the NEXT
2052
- // non-degraded attempt on the SAME OAuth credential over dollars that
2053
- // credential never spent. NO fallback to `rateLimitBucketId` on this
2133
+ // non-degraded attempt on the SAME OAuth credential over usage that
2134
+ // credential never made. NO fallback to `rateLimitBucketId` on this
2054
2135
  // branch: the constructor refuses to build a `reservesSpend: false` +
2055
2136
  // `spendLedger` harness without `degradedProjectionBucketId` (see the
2056
2137
  // throw in the constructor), so a silent wrong-bucket fence can never
@@ -2067,7 +2148,6 @@ export class StepExecutionHarness implements StepRunner {
2067
2148
  reservation = await this.spendLedger.reserve(
2068
2149
  {
2069
2150
  bucketId,
2070
- reservedUsd: this.reservedUsdPerAttempt,
2071
2151
  sessionId: request.sessionId,
2072
2152
  attempt,
2073
2153
  },
@@ -2103,86 +2183,16 @@ export class StepExecutionHarness implements StepRunner {
2103
2183
  harnessWatchdogMs,
2104
2184
  });
2105
2185
 
2106
- // Step 4: settle actual known cost exactly once (or route to
2107
- // markUnresolvedRemote when usage completeness never became
2108
- // "complete" — coupling 1, spend-limiter.ts).
2186
+ // Step 4: settle exactly once (or route to markUnresolvedRemote when
2187
+ // usage completeness never became "complete" — coupling 1,
2188
+ // spend-limiter.ts).
2109
2189
  if (reservation === undefined) return result;
2110
2190
  const finalized = await this.finalizeReservation(
2111
2191
  reservation,
2112
2192
  result,
2113
2193
  transport,
2114
2194
  );
2115
- // #182 follow-up fail-fast: a free-declared route that settled with
2116
- // reason `free_nonzero_cost` means the model flipped from free to
2117
- // metered mid-flight and the provider priced the work. Unresolved alone
2118
- // only fences the bucket while the retry loop keeps spending attempts on
2119
- // the flipped model, so the attempt FAILS CLOSED here with no retry —
2120
- // `legacy_terminal` breaks the run loop before `decideRetryDisposition`
2121
- // can resurrect it, and the fence from `markUnresolvedRemote` (already
2122
- // applied inside `finalizeReservation` — the spend may be real) blocks
2123
- // the next reserve. Free usage with NO cash figure carries no reason and
2124
- // releases without fencing (finalizeReservation's free-route fence
2125
- // scope) — no cash figure means no evidence of billing. Incomplete
2126
- // usage with a non-free mode still fences: no usage mode to exonerate
2127
- // it, so fail closed.
2128
- if (
2129
- finalized.decision.kind === "unresolved" &&
2130
- finalized.decision.reason === "free_nonzero_cost"
2131
- ) {
2132
- const known =
2133
- finalized.decision.knownUsd ?? finalized.reservation.knownUsd;
2134
- const flipNote =
2135
- `[pr-hero] free-route cost flip: provider reported $${known} cash ` +
2136
- `on a free-declared route; the model may have flipped from free to ` +
2137
- `metered — re-probe before retrying (no retry attempted)`;
2138
- if (result.kind === "cancelled") {
2139
- return {
2140
- kind: "failed",
2141
- outcome: {
2142
- completion: "failed",
2143
- protocolIntegrity: "unverified",
2144
- finalText: "",
2145
- usage: normalizeUnavailableUsage({ wallMs: 0 }),
2146
- stderrTail: flipNote,
2147
- },
2148
- resolution: { kind: "legacy_terminal" },
2149
- reservation: finalized.reservation,
2150
- };
2151
- }
2152
- // A DELIVERED attempt already persisted its parsed output
2153
- // (writeJsonAtomically in runAdmittedAttempt) and the loop returns ok
2154
- // on delivered without consulting the resolution — so rewriting it to
2155
- // failed would claim an artifact was never written. Preserve the
2156
- // delivered kind and append the flip note to stderrTail instead. The
2157
- // fence is already applied above, and no retry follows delivered
2158
- // anyway, so nothing about fail-closed is weakened: the next reserve
2159
- // on this bucket still refuses.
2160
- if (result.kind === "delivered") {
2161
- return {
2162
- kind: "delivered",
2163
- outcome: {
2164
- ...result.outcome,
2165
- stderrTail: [result.outcome.stderrTail, flipNote]
2166
- .filter(Boolean)
2167
- .join("\n"),
2168
- },
2169
- parsed: result.parsed,
2170
- reservation: finalized.reservation,
2171
- };
2172
- }
2173
- return {
2174
- kind: "failed",
2175
- outcome: {
2176
- ...result.outcome,
2177
- stderrTail: [result.outcome.stderrTail, flipNote]
2178
- .filter(Boolean)
2179
- .join("\n"),
2180
- },
2181
- resolution: { kind: "legacy_terminal" },
2182
- reservation: finalized.reservation,
2183
- };
2184
- }
2185
- return { ...result, reservation: finalized.reservation };
2195
+ return { ...result, reservation: finalized };
2186
2196
  } catch (error) {
2187
2197
  if (
2188
2198
  error instanceof ConcurrencyAdmissionAbortedError ||
@@ -2214,111 +2224,64 @@ export class StepExecutionHarness implements StepRunner {
2214
2224
  // `settlementFromUsage` and applies whichever CAS transition it names.
2215
2225
  // `result.kind === "cancelled"` here means the provider attempt WAS
2216
2226
  // invoked (executeSession calls the transport unconditionally) but no
2217
- // usage ever arrived — that case can never settle as a number either, so
2218
- // it takes the same unresolved path with `knownUsd: undefined`.
2219
- //
2220
- // Returns the decision alongside the terminal reservation so `runAttempt`
2221
- // can fail closed on `free_nonzero_cost` (#182 follow-up) — the only caller.
2227
+ // usage ever arrived — that case can never settle either, so it takes the
2228
+ // same unresolved path.
2222
2229
  private async finalizeReservation(
2223
2230
  reservation: SpendReservation,
2224
2231
  result: AttemptRunResult,
2225
2232
  transport?: ProviderTransport,
2226
- ): Promise<{
2227
- readonly reservation: SpendReservation;
2228
- readonly decision: SettlementDecision;
2229
- }> {
2233
+ ): Promise<SpendReservation> {
2230
2234
  const ledger = this.spendLedger;
2231
2235
  // Unreachable via `runAttempt` (it returns before calling here when no
2232
2236
  // reservation was opened, which is exactly when no ledger is configured),
2233
- // but kept total: report the pure decision without applying any CAS
2234
- // transition, so the shape holds and no fence is claimed.
2235
- if (ledger === undefined) {
2236
- const usage =
2237
- result.kind === "cancelled" ? undefined : result.outcome.usage;
2238
- return {
2239
- reservation,
2240
- decision:
2241
- usage === undefined
2242
- ? { kind: "unresolved", knownUsd: undefined }
2243
- : settlementFromUsage(usage),
2244
- };
2245
- }
2237
+ // but kept total: no CAS transition is applied and no fence is claimed.
2238
+ if (ledger === undefined) return reservation;
2246
2239
  const idempotencyKey = `${reservation.reservationId}:finalize`;
2247
2240
  const usage =
2248
2241
  result.kind === "cancelled" ? undefined : result.outcome.usage;
2249
2242
  if (usage === undefined) {
2250
2243
  await ledger.markUnresolvedRemote(
2251
2244
  reservation.reservationId,
2252
- undefined,
2253
2245
  idempotencyKey,
2254
2246
  );
2255
- return {
2256
- reservation: {
2257
- ...reservation,
2258
- state: "unresolved_remote",
2259
- knownUsd: undefined,
2260
- },
2261
- decision: { kind: "unresolved", knownUsd: undefined },
2262
- };
2247
+ return { ...reservation, state: "unresolved_remote" };
2263
2248
  }
2264
2249
  const decision = settlementFromUsage(usage);
2265
2250
  if (decision.kind === "settle") {
2266
2251
  await ledger.settle(reservation.reservationId, decision, idempotencyKey);
2267
- return {
2268
- reservation: {
2269
- ...reservation,
2270
- state: "settled",
2271
- settledUsd: decision.actualUsd,
2272
- },
2273
- decision,
2274
- };
2252
+ return { ...reservation, state: "settled" };
2275
2253
  }
2276
- // #182 follow-up, free-route fence scope: a free binding reserves so the
2277
- // free-nonzero rule can fence on a priced flip — but that made EVERY
2278
- // terminal free outcome fence the shared credential bucket, so one
2279
- // transient failure (parse failure, watchdog timeout shape) killed the
2280
- // whole all-free run. That contradicts the neighboring subscription
2281
- // reasoning in production-runtime.ts MultiProviderRunner.run: on a route
2282
- // that bills nothing there are no dollars to stop spending over.
2254
+ // #182 follow-up, free-route fence scope: a free binding reserves, but
2255
+ // fencing the shared credential bucket on EVERY terminal free outcome
2256
+ // meant one transient failure (parse failure, watchdog timeout shape)
2257
+ // killed the whole all-free run. That contradicts the neighboring
2258
+ // subscription reasoning in production-runtime.ts MultiProviderRunner.run:
2259
+ // a route that bills nothing has no credential worth protecting.
2283
2260
  //
2284
- // So an unresolved free outcome WITHOUT reason releases instead of
2285
- // fencing: no cash figure means no spend evidence on a free route, and a
2286
- // terminal released_unstarted fences nothing — the same-step retry
2287
- // reserves fine and sibling steps are unaffected. Coupling-1 is
2288
- // preserved: a non-complete usage still never settles as a number, it
2289
- // just releases instead of fencing. Cancelled/undefined-usage keeps the
2290
- // fence (no usage means no evidence either way — fail closed), the
2291
- // free_nonzero_cost reason keeps the fence (priced work may be real), and
2292
- // the metered path below is untouched. Downstream-safe:
2293
- // collectUnresolvedSpend (pipeline.ts) and renderResult (ui/result.ts)
2294
- // only read unresolved_remote reservations, so a released_unstarted free
2295
- // attempt is invisible to both — no floor marker, no unresolved row.
2261
+ // So an unresolved free outcome releases instead of fencing: a terminal
2262
+ // released_unstarted fences nothing — the same-step retry reserves fine
2263
+ // and sibling steps are unaffected. Coupling-1 is preserved: a non-complete
2264
+ // usage still never settles, it just releases instead of fencing.
2265
+ // Cancelled/undefined-usage keeps the fence (no usage means no evidence
2266
+ // either way — fail closed), and the metered path below is untouched.
2267
+ // Downstream-safe: collectUnresolvedSpend (pipeline.ts) and renderResult
2268
+ // (ui/result.ts) only read unresolved_remote reservations, so a
2269
+ // released_unstarted free attempt is invisible to both.
2296
2270
  //
2297
2271
  // #187: watchdog timeout on a free route synthesizes an unavailable usage;
2298
2272
  // preserving the transport's free billing mode ensures it releases
2299
2273
  // unstarted instead of fencing the bucket, allowing attempt 2 to retry.
2300
2274
  const isFreeRoute =
2301
2275
  usage.billingMode === "free" || transport?.billingMode === "free";
2302
- if (isFreeRoute && decision.reason === undefined) {
2276
+ if (isFreeRoute) {
2303
2277
  await ledger.releaseUnstarted(reservation.reservationId, idempotencyKey);
2304
- return {
2305
- reservation: { ...reservation, state: "released_unstarted" },
2306
- decision,
2307
- };
2278
+ return { ...reservation, state: "released_unstarted" };
2308
2279
  }
2309
2280
  await ledger.markUnresolvedRemote(
2310
2281
  reservation.reservationId,
2311
- decision.knownUsd,
2312
2282
  idempotencyKey,
2313
2283
  );
2314
- return {
2315
- reservation: {
2316
- ...reservation,
2317
- state: "unresolved_remote",
2318
- knownUsd: decision.knownUsd,
2319
- },
2320
- decision,
2321
- };
2284
+ return { ...reservation, state: "unresolved_remote" };
2322
2285
  }
2323
2286
 
2324
2287
  // The body of a single attempt, unchanged from PR0 except for the §9.2
@@ -2541,7 +2504,16 @@ export class StepExecutionHarness implements StepRunner {
2541
2504
  if (execution.cancelled) {
2542
2505
  // A cancelled attempt has no outcome to carry a warning, but what it did
2543
2506
  // before the stop is still worth keeping.
2544
- await copyClaudeSessionTrace(step, request, attempt);
2507
+ const miss = await copyClaudeSessionTrace(step, request, attempt);
2508
+ await writeAttemptUsageFacts({
2509
+ step,
2510
+ route: request.route,
2511
+ attempt,
2512
+ transport,
2513
+ miss,
2514
+ outcome: undefined,
2515
+ billingMode: this.attemptBillingMode(transport),
2516
+ });
2545
2517
  return { kind: "cancelled" };
2546
2518
  }
2547
2519
 
@@ -2549,12 +2521,18 @@ export class StepExecutionHarness implements StepRunner {
2549
2521
  // `execution.cancelled` is the only path executeSession takes without
2550
2522
  // ever invoking `onData`, and `onData` is what both delivery variants
2551
2523
  // above come from.
2552
- const outcome = await withClaudeSessionTrace(
2524
+ const settled = execution.outcome as TransportOutcome;
2525
+ const miss = await copyClaudeSessionTrace(step, request, attempt);
2526
+ await writeAttemptUsageFacts({
2553
2527
  step,
2554
- request,
2528
+ route: request.route,
2555
2529
  attempt,
2556
- execution.outcome as TransportOutcome,
2557
- );
2530
+ transport,
2531
+ miss,
2532
+ outcome: settled,
2533
+ billingMode: settled.usage.billingMode,
2534
+ });
2535
+ const outcome = withTraceMissAnnotation(settled, miss);
2558
2536
 
2559
2537
  if (execution.delivery?.delivered) {
2560
2538
  return { kind: "delivered", outcome, parsed: execution.delivery.parsed };