crysk 0.0.0 → 0.3.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/LICENSE +176 -0
- package/README.md +609 -3
- package/bin/pr-hero.js +166 -0
- package/config/models/anthropic.json +5 -0
- package/dist/SKILL-7e3725zn.md +496 -0
- package/dist/SKILL-7q35wcaz.md +66 -0
- package/dist/SKILL-9at22381.md +74 -0
- package/dist/SKILL-ex2hxxh3.md +134 -0
- package/dist/adjudicator-dtg2axnh.md +86 -0
- package/dist/admission-config.example-a9vqmmad.json +9 -0
- package/dist/ci-admission-camfbx2b.md +122 -0
- package/dist/cli.js +42292 -0
- package/dist/consolidate-ac0mnfyj.md +24 -0
- package/dist/deep-review-lifecycle-m698ybjp.md +362 -0
- package/dist/deep-review-logic-a6b1gczf.md +156 -0
- package/dist/deep-review-parity-0ec904db.md +120 -0
- package/dist/deep-review-reliability-qyakbjb9.md +125 -0
- package/dist/deep-review-resilience-3tz8y26e.md +118 -0
- package/dist/index.js +20190 -0
- package/dist/opencode-ci-7b0be64p.md +141 -0
- package/dist/review-refuter-j4e9gezy.md +71 -0
- package/dist/scout-yvp153q7.md +140 -0
- package/dist/summarizer-zca44nxa.md +18 -0
- package/dist/workflow-w1g9szna.yml +151 -0
- package/package.json +81 -4
- package/prompts/consolidate.md +24 -0
- package/prompts/default/PROVENANCE.md +18 -0
- package/prompts/default/deep-review-lifecycle.md +362 -0
- package/prompts/default/deep-review-logic.md +156 -0
- package/prompts/default/deep-review-parity.md +120 -0
- package/prompts/default/deep-review-reliability.md +125 -0
- package/prompts/default/deep-review-resilience.md +118 -0
- package/prompts/default/review-refuter.md +71 -0
- package/prompts/scout.md +140 -0
- package/prompts/summarizer.md +18 -0
- package/skills/crysk/SKILL.md +74 -0
- package/skills/crysk-ci-setup/SKILL.md +134 -0
- package/skills/crysk-ci-setup/assets/admission-config.example.json +9 -0
- package/skills/crysk-ci-setup/assets/workflow.yml +151 -0
- package/skills/crysk-ci-setup/references/ci-admission.md +122 -0
- package/skills/crysk-ci-setup/references/opencode-ci.md +141 -0
- package/skills/crysk-loop/SKILL.md +496 -0
- package/skills/crysk-loop/adjudicator.md +86 -0
- package/skills/crysk-pr/SKILL.md +66 -0
- package/src/agent-env.ts +1084 -0
- package/src/asset-manifest.ts +83 -0
- package/src/assets.d.ts +6 -0
- package/src/assets.ts +183 -0
- package/src/brand-paths.ts +155 -0
- package/src/brand.ts +255 -0
- package/src/ci/admission-ledger.ts +350 -0
- package/src/ci/classify-error.ts +73 -0
- package/src/ci/cli-record.ts +271 -0
- package/src/ci/force-comment.ts +39 -0
- package/src/ci/gates.ts +923 -0
- package/src/ci/outcome.ts +2181 -0
- package/src/ci/override.ts +187 -0
- package/src/ci/publish.ts +1639 -0
- package/src/ci/reporter.ts +626 -0
- package/src/ci/review-admission.ts +901 -0
- package/src/ci/review-levels.ts +310 -0
- package/src/ci/review-policy.ts +18 -0
- package/src/ci/review-risk.ts +184 -0
- package/src/ci/setup.ts +844 -0
- package/src/ci/trigger.ts +1353 -0
- package/src/cli.ts +348 -0
- package/src/commands/activity.ts +67 -0
- package/src/commands/ci-setup-questions.ts +397 -0
- package/src/commands/ci-setup.ts +381 -0
- package/src/commands/config.ts +99 -0
- package/src/commands/doctor.ts +108 -0
- package/src/commands/init.ts +139 -0
- package/src/commands/mcp.ts +10 -0
- package/src/commands/menu.ts +129 -0
- package/src/commands/post.ts +302 -0
- package/src/commands/skills.ts +83 -0
- package/src/commands/status.ts +173 -0
- package/src/commands/triage.ts +342 -0
- package/src/commands/uninstall.ts +37 -0
- package/src/commands/upgrade.ts +214 -0
- package/src/commands/usage.ts +98 -0
- package/src/compare/compare.ts +28 -0
- package/src/compare/floor-test.ts +471 -0
- package/src/compare/martian-adapter.ts +80 -0
- package/src/config/config.ts +267 -0
- package/src/corpus/corpus.ts +1094 -0
- package/src/corpus/preflight.ts +1789 -0
- package/src/diversity/accounting.ts +90 -0
- package/src/diversity/adjudication.ts +145 -0
- package/src/diversity/admission.ts +60 -0
- package/src/diversity/benchmark.ts +195 -0
- package/src/diversity/clustering.ts +285 -0
- package/src/diversity/errors.ts +7 -0
- package/src/diversity/identity.ts +285 -0
- package/src/diversity/index.ts +11 -0
- package/src/diversity/pipeline-integration.ts +448 -0
- package/src/diversity/projection.ts +57 -0
- package/src/diversity/self-contained.ts +68 -0
- package/src/diversity/target-validation.ts +179 -0
- package/src/doctor-migration.ts +209 -0
- package/src/doctor.ts +723 -0
- package/src/errors.ts +83 -0
- package/src/execution/admission.ts +75 -0
- package/src/execution/atomic-write.ts +35 -0
- package/src/execution/attempt-evidence.ts +415 -0
- package/src/execution/bucket-id.ts +136 -0
- package/src/execution/concurrency-limiter.ts +175 -0
- package/src/execution/contracts.ts +443 -0
- package/src/execution/failure-classification.ts +42 -0
- package/src/execution/failure-policy.ts +240 -0
- package/src/execution/harness.ts +2548 -0
- package/src/execution/settlement.ts +348 -0
- package/src/execution/spawned-process.ts +66 -0
- package/src/execution/spend-limiter.ts +424 -0
- package/src/execution/step-artifacts.ts +55 -0
- package/src/execution/usage-normalized.ts +469 -0
- package/src/git/git.ts +396 -0
- package/src/git/identity.ts +64 -0
- package/src/git/refs.ts +140 -0
- package/src/home-preflight.ts +443 -0
- package/src/home.ts +243 -0
- package/src/ignore-file.ts +336 -0
- package/src/ignore-read.ts +118 -0
- package/src/index.ts +34 -0
- package/src/legacy-launchd.ts +29 -0
- package/src/mcp/mcp.ts +150 -0
- package/src/mcp/preflight.ts +407 -0
- package/src/menu-context.ts +192 -0
- package/src/model/catalog.ts +251 -0
- package/src/model/exec-snapshots.ts +390 -0
- package/src/model/free-discovery.ts +302 -0
- package/src/model/provider-capabilities.ts +814 -0
- package/src/model/routing.ts +600 -0
- package/src/pr/admission.ts +847 -0
- package/src/pr/ci-admission-gate.ts +665 -0
- package/src/pr/ci-publish.ts +76 -0
- package/src/pr/discovery.ts +432 -0
- package/src/pr/inline.ts +507 -0
- package/src/pr/plan.ts +366 -0
- package/src/pr/posting.ts +115 -0
- package/src/pr/pr-status.ts +787 -0
- package/src/pr/pr.ts +3642 -0
- package/src/pr/preflight.ts +1031 -0
- package/src/pr/publish-outcome.ts +361 -0
- package/src/pr/range.ts +119 -0
- package/src/pr/reverts-preflight.ts +606 -0
- package/src/pr/reverts.ts +377 -0
- package/src/pr/review-pr.ts +927 -0
- package/src/pr/status.ts +154 -0
- package/src/pr/target.ts +388 -0
- package/src/pr/teardown.ts +437 -0
- package/src/pr/worktree-setup.ts +60 -0
- package/src/production-runtime.ts +1877 -0
- package/src/progress.ts +487 -0
- package/src/rereview/adjudicate.ts +89 -0
- package/src/rereview/classify.ts +362 -0
- package/src/rereview/identity.ts +211 -0
- package/src/rereview/plan.ts +308 -0
- package/src/rereview/prepare.ts +1025 -0
- package/src/rereview/state.ts +767 -0
- package/src/rereview/verify.ts +241 -0
- package/src/review/boundary.ts +105 -0
- package/src/review/consolidate.ts +246 -0
- package/src/review/dedupe.ts +368 -0
- package/src/review/drafts.ts +448 -0
- package/src/review/findings-conformance.ts +120 -0
- package/src/review/findings.ts +714 -0
- package/src/review/pipeline.ts +3977 -0
- package/src/review/preflight.ts +3575 -0
- package/src/review/prompt-set.ts +177 -0
- package/src/review/proof-refs.ts +187 -0
- package/src/review/report.ts +1821 -0
- package/src/review/review.ts +490 -0
- package/src/review/root-cause.ts +120 -0
- package/src/review/route-preflight.ts +289 -0
- package/src/review/run.ts +601 -0
- package/src/review/scout.ts +323 -0
- package/src/review/size-gate.ts +595 -0
- package/src/review/spec.ts +235 -0
- package/src/review/step-runner.ts +233 -0
- package/src/runner-authority.ts +596 -0
- package/src/security/binding-policy.ts +115 -0
- package/src/security/credential-broker.ts +773 -0
- package/src/security/evidence-redaction.ts +62 -0
- package/src/security/execution-authority.ts +27 -0
- package/src/security/redact.ts +71 -0
- package/src/security/workspace-read-broker.ts +466 -0
- package/src/server/client.ts +190 -0
- package/src/server/preflight.ts +217 -0
- package/src/server/server.ts +327 -0
- package/src/signal-handler.ts +50 -0
- package/src/store/activity.ts +413 -0
- package/src/store/backfill-preflight.ts +49 -0
- package/src/store/backfill.ts +141 -0
- package/src/store/gc-preflight.ts +206 -0
- package/src/store/gc.ts +424 -0
- package/src/store/metrics-preflight.ts +229 -0
- package/src/store/metrics.ts +236 -0
- package/src/store/preflight.ts +748 -0
- package/src/store/store.ts +693 -0
- package/src/system-tools.ts +355 -0
- package/src/transport-registry.ts +1124 -0
- package/src/transports/claude-code-cli.ts +1348 -0
- package/src/transports/opencode-admission.ts +515 -0
- package/src/transports/opencode-client.ts +3328 -0
- package/src/transports/opencode-evidence.ts +557 -0
- package/src/transports/opencode-mcp.ts +248 -0
- package/src/transports/opencode-sdk.ts +2294 -0
- package/src/transports/opencode-server.ts +535 -0
- package/src/triage/adjudication.ts +113 -0
- package/src/triage/reply.ts +125 -0
- package/src/triage/triage.ts +255 -0
- package/src/ui/activity.ts +173 -0
- package/src/ui/config-edit.ts +816 -0
- package/src/ui/config.ts +462 -0
- package/src/ui/menu.ts +1325 -0
- package/src/ui/plan.ts +1039 -0
- package/src/ui/primitives.ts +300 -0
- package/src/ui/progress.ts +298 -0
- package/src/ui/result.ts +552 -0
- package/src/ui/review-menu.ts +312 -0
- package/src/ui/select.ts +528 -0
- package/src/ui/tree.ts +178 -0
- package/src/uninstaller.ts +479 -0
- package/src/updater.ts +836 -0
- package/src/usage.ts +77 -0
- package/src/watch/preflight.ts +1389 -0
- package/src/watch/watch.ts +1235 -0
- package/src/wizard.ts +877 -0
|
@@ -0,0 +1,557 @@
|
|
|
1
|
+
import type { DiagnosticEvidence } from "../execution/contracts";
|
|
2
|
+
|
|
3
|
+
import { redactEvidence } from "../security/evidence-redaction";
|
|
4
|
+
|
|
5
|
+
export {
|
|
6
|
+
redactEvidence,
|
|
7
|
+
redactEvidenceText,
|
|
8
|
+
} from "../security/evidence-redaction";
|
|
9
|
+
|
|
10
|
+
const MAX_BYTES = 4 * 1024 * 1024;
|
|
11
|
+
const MAX_RECORDS = 10000;
|
|
12
|
+
// #228: a real production capture hit the 10,000-record cap at ~57.7s into a
|
|
13
|
+
// 242-286s attempt — the silence window that needed diagnosing happened
|
|
14
|
+
// AFTER the cap was already exhausted, so stopping at the cap threw away
|
|
15
|
+
// exactly the part that mattered. The first HEAD_MAX_RECORDS/HEAD_MAX_BYTES
|
|
16
|
+
// (whichever comes first) are kept PERMANENTLY — this is where
|
|
17
|
+
// session-identity and the prompt's http_request/response/request_body
|
|
18
|
+
// naturally land, since they happen at the very start of a turn. Everything
|
|
19
|
+
// after that is a rolling TAIL of the most recent records, which is where a
|
|
20
|
+
// terminal readback naturally lands, since it happens at the very end.
|
|
21
|
+
const HEAD_MAX_RECORDS = 1000;
|
|
22
|
+
const HEAD_MAX_BYTES = 1 * 1024 * 1024;
|
|
23
|
+
// PR #228 review, F001: `fits()` used to reserve marker room only once
|
|
24
|
+
// `elidedCount > 0` — i.e. only AFTER something had already been elided.
|
|
25
|
+
// But the oversized-record pre-check and the head-admission check run the
|
|
26
|
+
// identical formula for the same record at the same instant while
|
|
27
|
+
// `elidedCount === 0`, so the record that fails one identically fails the
|
|
28
|
+
// other and is always skipped before it can land in an empty tail. That
|
|
29
|
+
// means the FIRST-EVER skip's marker is born the instant a head+tail
|
|
30
|
+
// combination was already accepted WITHOUT it ever being budgeted for —
|
|
31
|
+
// and head is permanent, so nothing can shrink to make room afterward.
|
|
32
|
+
// Reserving this worst-case serialized marker size unconditionally, from
|
|
33
|
+
// the very first record on, keeps every accepted state already compatible
|
|
34
|
+
// with a marker that has not been created yet, so `snapshot()` can never
|
|
35
|
+
// exceed `maxBytes` regardless of when or how many times elision starts.
|
|
36
|
+
// Worst case: `observedMs` is the longest a JSON number round-trips to
|
|
37
|
+
// (the most negative representable double), and `records`/`bytes` are
|
|
38
|
+
// each bounded by `Number.MAX_SAFE_INTEGER` — no real capture count or
|
|
39
|
+
// byte tally can ever legally exceed that.
|
|
40
|
+
const MARKER_RESERVE_BYTES = Buffer.byteLength(
|
|
41
|
+
JSON.stringify({
|
|
42
|
+
seq: 0,
|
|
43
|
+
observedMs: -1.7976931348623157e308,
|
|
44
|
+
kind: "elided",
|
|
45
|
+
data: { records: Number.MAX_SAFE_INTEGER, bytes: Number.MAX_SAFE_INTEGER },
|
|
46
|
+
}),
|
|
47
|
+
);
|
|
48
|
+
export interface OpenCodeObservation {
|
|
49
|
+
seq: number;
|
|
50
|
+
observedMs: number;
|
|
51
|
+
kind: string;
|
|
52
|
+
data: unknown;
|
|
53
|
+
}
|
|
54
|
+
interface TailEntry {
|
|
55
|
+
readonly record: OpenCodeObservation;
|
|
56
|
+
readonly size: number;
|
|
57
|
+
}
|
|
58
|
+
export class OpenCodeEvidenceCollector {
|
|
59
|
+
private head: OpenCodeObservation[] = [];
|
|
60
|
+
private headBytes = 0;
|
|
61
|
+
private headClosed = false;
|
|
62
|
+
private tail: TailEntry[] = [];
|
|
63
|
+
private tailBytes = 0;
|
|
64
|
+
private elidedCount = 0;
|
|
65
|
+
private elidedBytes = 0;
|
|
66
|
+
// Fixed the FIRST time anything is ever elided, and reused for every
|
|
67
|
+
// subsequent marker-size estimate AND the final emitted marker — so the
|
|
68
|
+
// marker's own serialized size, once it exists, never drifts between the
|
|
69
|
+
// budget decisions made while collecting and what `snapshot()` actually
|
|
70
|
+
// writes. Growth in `elidedCount`/`elidedBytes` (their digit width) is the
|
|
71
|
+
// only thing allowed to change the marker's size across those checks.
|
|
72
|
+
private elidedObservedMs?: number;
|
|
73
|
+
private nextSeq = 1;
|
|
74
|
+
private incomplete = false;
|
|
75
|
+
private pending = 0;
|
|
76
|
+
private frozen?: DiagnosticEvidence;
|
|
77
|
+
private wrapperBytesCache?: number;
|
|
78
|
+
constructor(
|
|
79
|
+
private correlation: { sessionId: string; attempt: number },
|
|
80
|
+
private maxBytes = MAX_BYTES,
|
|
81
|
+
) {}
|
|
82
|
+
// `snapshot()` serializes `{"schemaVersion":1,...correlation,"records":[...]}`
|
|
83
|
+
// — every record beyond the first costs one extra byte for its join comma,
|
|
84
|
+
// and the object wrapper itself (schemaVersion/correlation/brackets) costs a
|
|
85
|
+
// fixed amount too. `record()` used to reserve a flat 256-byte slack for
|
|
86
|
+
// both, which a few thousand records blow through on commas alone (each is
|
|
87
|
+
// 1 byte, but there is one per record past the first) — the final
|
|
88
|
+
// `redactedJson` could then land past `maxBytes` even though every
|
|
89
|
+
// individual record fit under it. Computed once here (the wrapper shape
|
|
90
|
+
// never changes after construction) so `record()` can budget the exact
|
|
91
|
+
// overhead instead of guessing at it.
|
|
92
|
+
private wrapperBytes(): number {
|
|
93
|
+
if (this.wrapperBytesCache === undefined)
|
|
94
|
+
this.wrapperBytesCache = Buffer.byteLength(
|
|
95
|
+
JSON.stringify({
|
|
96
|
+
schemaVersion: 1,
|
|
97
|
+
...this.correlation,
|
|
98
|
+
records: [],
|
|
99
|
+
}),
|
|
100
|
+
);
|
|
101
|
+
return this.wrapperBytesCache;
|
|
102
|
+
}
|
|
103
|
+
private markerRecord(): OpenCodeObservation {
|
|
104
|
+
return {
|
|
105
|
+
seq: 0, // never collides with a real record's seq, which starts at 1
|
|
106
|
+
observedMs: this.elidedObservedMs ?? 0,
|
|
107
|
+
kind: "elided",
|
|
108
|
+
data: { records: this.elidedCount, bytes: this.elidedBytes },
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
// Exact accounting for the FINAL emitted sequence (head, then the marker
|
|
112
|
+
// if anything was ever elided, then tail) given candidate head/tail
|
|
113
|
+
// sizes — the same join-comma/wrapper formula `record()`'s overhead fix
|
|
114
|
+
// established, extended to a marker that may or may not exist yet.
|
|
115
|
+
private fits(
|
|
116
|
+
headCount: number,
|
|
117
|
+
headBytes: number,
|
|
118
|
+
tailCount: number,
|
|
119
|
+
tailBytes: number,
|
|
120
|
+
): boolean {
|
|
121
|
+
// Always reserve one record slot and MARKER_RESERVE_BYTES for a marker
|
|
122
|
+
// that may not exist yet (see MARKER_RESERVE_BYTES above) — never
|
|
123
|
+
// conditioned on `this.elidedCount > 0`, since that is exactly the
|
|
124
|
+
// instant this check cannot yet know whether the marker is about to be
|
|
125
|
+
// born. The real marker, once it exists, is always <=
|
|
126
|
+
// MARKER_RESERVE_BYTES, so this is a safe over-reservation, not a
|
|
127
|
+
// separate re-check.
|
|
128
|
+
const totalCount = headCount + 1 + tailCount;
|
|
129
|
+
if (totalCount > MAX_RECORDS) return false;
|
|
130
|
+
const totalBytes =
|
|
131
|
+
this.wrapperBytes() +
|
|
132
|
+
headBytes +
|
|
133
|
+
MARKER_RESERVE_BYTES +
|
|
134
|
+
tailBytes +
|
|
135
|
+
Math.max(0, totalCount - 1);
|
|
136
|
+
return totalBytes <= this.maxBytes;
|
|
137
|
+
}
|
|
138
|
+
record(kind: string, data: unknown): void {
|
|
139
|
+
if (this.frozen) return;
|
|
140
|
+
try {
|
|
141
|
+
const safe = redactEvidence(data);
|
|
142
|
+
const rec: OpenCodeObservation = {
|
|
143
|
+
seq: this.nextSeq,
|
|
144
|
+
observedMs: performance.now(),
|
|
145
|
+
kind,
|
|
146
|
+
data: safe,
|
|
147
|
+
};
|
|
148
|
+
const size = Buffer.byteLength(JSON.stringify(rec));
|
|
149
|
+
|
|
150
|
+
if (!this.headClosed) {
|
|
151
|
+
const withinHeadBudget =
|
|
152
|
+
this.head.length + 1 <= HEAD_MAX_RECORDS &&
|
|
153
|
+
this.headBytes + size <= HEAD_MAX_BYTES;
|
|
154
|
+
if (
|
|
155
|
+
withinHeadBudget &&
|
|
156
|
+
this.fits(this.head.length + 1, this.headBytes + size, 0, 0)
|
|
157
|
+
) {
|
|
158
|
+
this.head.push(rec);
|
|
159
|
+
this.headBytes += size;
|
|
160
|
+
this.nextSeq++;
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
this.headClosed = true;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// A record too large to fit even an EMPTY tail must be skipped BEFORE
|
|
167
|
+
// touching the existing tail at all. The old code found this out only
|
|
168
|
+
// by draining every real tail record first (the loop below) and then
|
|
169
|
+
// throwing "capture limit" once there was nothing left to evict — and
|
|
170
|
+
// since that throw landed in the catch below, which used to also set
|
|
171
|
+
// the early-return gate above, ONE oversized record (a multi-MB
|
|
172
|
+
// response_body at the end of an attempt is the realistic case) both
|
|
173
|
+
// erased every tail record that survived up to that point AND
|
|
174
|
+
// silenced every record for the rest of the attempt. Checked here so
|
|
175
|
+
// the existing tail is never touched for a record that could never
|
|
176
|
+
// have fit it regardless of what got evicted.
|
|
177
|
+
if (!this.fits(this.head.length, this.headBytes, 1, size)) {
|
|
178
|
+
if (this.elidedCount === 0) this.elidedObservedMs = performance.now();
|
|
179
|
+
this.elidedCount += 1;
|
|
180
|
+
this.elidedBytes += size;
|
|
181
|
+
this.incomplete = true;
|
|
182
|
+
this.nextSeq++;
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Tail phase: append `rec`, evicting the oldest tail entries first if
|
|
187
|
+
// needed to stay within both the record cap and `maxBytes` — the
|
|
188
|
+
// moment the FIRST eviction happens, a marker starts existing, and
|
|
189
|
+
// every fit check from then on (including this same loop's later
|
|
190
|
+
// iterations) accounts for it.
|
|
191
|
+
let candidateTailBytes = this.tailBytes + size;
|
|
192
|
+
let candidateTailCount = this.tail.length + 1;
|
|
193
|
+
for (;;) {
|
|
194
|
+
if (
|
|
195
|
+
this.fits(
|
|
196
|
+
this.head.length,
|
|
197
|
+
this.headBytes,
|
|
198
|
+
candidateTailCount,
|
|
199
|
+
candidateTailBytes,
|
|
200
|
+
)
|
|
201
|
+
) {
|
|
202
|
+
this.tail.push({ record: rec, size });
|
|
203
|
+
this.tailBytes = candidateTailBytes;
|
|
204
|
+
this.nextSeq++;
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
const oldest = this.tail.shift();
|
|
208
|
+
// Unreachable given the pre-check above already proved the record
|
|
209
|
+
// fits an EMPTY tail: this loop can only run out of entries to evict
|
|
210
|
+
// before `fits()` turns true if that proof was wrong. Kept as a
|
|
211
|
+
// fail-safe — the catch below still marks `incomplete` rather than
|
|
212
|
+
// recording a wrong result, and no longer disables future recording.
|
|
213
|
+
if (oldest === undefined) throw new Error("capture limit");
|
|
214
|
+
this.tailBytes -= oldest.size;
|
|
215
|
+
if (this.elidedCount === 0) this.elidedObservedMs = performance.now();
|
|
216
|
+
this.elidedCount += 1;
|
|
217
|
+
this.elidedBytes += oldest.size;
|
|
218
|
+
candidateTailBytes = this.tailBytes + size;
|
|
219
|
+
candidateTailCount = this.tail.length + 1;
|
|
220
|
+
}
|
|
221
|
+
} catch {
|
|
222
|
+
this.incomplete = true;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
private body(
|
|
226
|
+
message: Request | Response,
|
|
227
|
+
kind: string,
|
|
228
|
+
requestId: number,
|
|
229
|
+
): void {
|
|
230
|
+
this.pending++;
|
|
231
|
+
void (async () => {
|
|
232
|
+
let reader: ReadableStreamDefaultReader<Uint8Array> | undefined;
|
|
233
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
234
|
+
try {
|
|
235
|
+
reader = message.body?.getReader();
|
|
236
|
+
if (!reader) {
|
|
237
|
+
this.record(kind, { requestId, body: null });
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
const chunks: Uint8Array[] = [];
|
|
241
|
+
let total = 0;
|
|
242
|
+
const deadline = new Promise<never>((_, reject) => {
|
|
243
|
+
timer = setTimeout(
|
|
244
|
+
() => reject(new Error("capture read deadline")),
|
|
245
|
+
100,
|
|
246
|
+
);
|
|
247
|
+
});
|
|
248
|
+
for (;;) {
|
|
249
|
+
const chunk = await Promise.race([reader.read(), deadline]);
|
|
250
|
+
if (chunk.done) break;
|
|
251
|
+
total += chunk.value.byteLength;
|
|
252
|
+
if (total > this.maxBytes) throw new Error("capture body limit");
|
|
253
|
+
chunks.push(chunk.value);
|
|
254
|
+
}
|
|
255
|
+
const text = Buffer.concat(chunks).toString("utf8");
|
|
256
|
+
let body: unknown = text;
|
|
257
|
+
try {
|
|
258
|
+
body = JSON.parse(text);
|
|
259
|
+
} catch {}
|
|
260
|
+
// Body is kept as the actual serialized object, correlated separately.
|
|
261
|
+
this.record(kind, { requestId, body });
|
|
262
|
+
} catch {
|
|
263
|
+
this.incomplete = true;
|
|
264
|
+
} finally {
|
|
265
|
+
clearTimeout(timer);
|
|
266
|
+
void reader?.cancel().catch(() => {});
|
|
267
|
+
this.pending--;
|
|
268
|
+
}
|
|
269
|
+
})();
|
|
270
|
+
}
|
|
271
|
+
wrapFetch(inner: (request: Request) => Promise<Response>): typeof fetch {
|
|
272
|
+
let requests = 0;
|
|
273
|
+
return (async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
274
|
+
const request =
|
|
275
|
+
input instanceof Request && init === undefined
|
|
276
|
+
? input
|
|
277
|
+
: new Request(input, init);
|
|
278
|
+
const requestId = ++requests;
|
|
279
|
+
this.record("http_request", {
|
|
280
|
+
requestId,
|
|
281
|
+
method: request.method,
|
|
282
|
+
url: request.url,
|
|
283
|
+
});
|
|
284
|
+
try {
|
|
285
|
+
if (request.body) this.body(request.clone(), "request_body", requestId);
|
|
286
|
+
} catch {
|
|
287
|
+
this.incomplete = true;
|
|
288
|
+
}
|
|
289
|
+
try {
|
|
290
|
+
const response = await inner(request);
|
|
291
|
+
this.record("http_response", { requestId, status: response.status });
|
|
292
|
+
if (
|
|
293
|
+
!response.headers.get("content-type")?.includes("text/event-stream")
|
|
294
|
+
) {
|
|
295
|
+
try {
|
|
296
|
+
this.body(response.clone(), "response_body", requestId);
|
|
297
|
+
} catch {
|
|
298
|
+
this.incomplete = true;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
return response;
|
|
302
|
+
} catch (error) {
|
|
303
|
+
this.record("http_error", {
|
|
304
|
+
requestId,
|
|
305
|
+
error: error instanceof Error ? error.message : "request failed",
|
|
306
|
+
});
|
|
307
|
+
throw error;
|
|
308
|
+
}
|
|
309
|
+
}) as typeof fetch;
|
|
310
|
+
}
|
|
311
|
+
snapshot(): DiagnosticEvidence {
|
|
312
|
+
if (!this.frozen) {
|
|
313
|
+
const records: OpenCodeObservation[] = [...this.head];
|
|
314
|
+
if (this.elidedCount > 0) records.push(this.markerRecord());
|
|
315
|
+
for (const entry of this.tail) records.push(entry.record);
|
|
316
|
+
this.frozen = {
|
|
317
|
+
schema: "pr-hero.opencode-observations.v1",
|
|
318
|
+
status:
|
|
319
|
+
this.incomplete || this.pending > 0 || this.elidedCount > 0
|
|
320
|
+
? "incomplete"
|
|
321
|
+
: "complete",
|
|
322
|
+
redactedJson: JSON.stringify({
|
|
323
|
+
schemaVersion: 1,
|
|
324
|
+
...this.correlation,
|
|
325
|
+
records,
|
|
326
|
+
}),
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
return this.frozen;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
interface CapturedMessage {
|
|
334
|
+
info: {
|
|
335
|
+
id?: string;
|
|
336
|
+
sessionID?: string;
|
|
337
|
+
role?: string;
|
|
338
|
+
parentID?: string;
|
|
339
|
+
path?: { cwd?: string };
|
|
340
|
+
error?: unknown;
|
|
341
|
+
finish?: string;
|
|
342
|
+
time?: { completed?: number };
|
|
343
|
+
};
|
|
344
|
+
parts: Array<{
|
|
345
|
+
id?: string;
|
|
346
|
+
messageID?: string;
|
|
347
|
+
sessionID?: string;
|
|
348
|
+
type?: string;
|
|
349
|
+
state?: { status?: string };
|
|
350
|
+
text?: unknown;
|
|
351
|
+
synthetic?: boolean;
|
|
352
|
+
ignored?: boolean;
|
|
353
|
+
}>;
|
|
354
|
+
}
|
|
355
|
+
export type EvidenceClassification =
|
|
356
|
+
| "demonstrated_reconstruction_defect"
|
|
357
|
+
| "persisted_final_empty"
|
|
358
|
+
| "external_rejection"
|
|
359
|
+
| "inconclusive";
|
|
360
|
+
/** Independent evidence check: raw readback plus known submitted ownership, never StepResult-derived facts. */
|
|
361
|
+
export function classifyObservationEvidence(
|
|
362
|
+
capture: DiagnosticEvidence | undefined,
|
|
363
|
+
localText: string,
|
|
364
|
+
): EvidenceClassification {
|
|
365
|
+
if (
|
|
366
|
+
capture?.status !== "complete" ||
|
|
367
|
+
capture.schema !== "pr-hero.opencode-observations.v1"
|
|
368
|
+
)
|
|
369
|
+
return "inconclusive";
|
|
370
|
+
try {
|
|
371
|
+
const data = JSON.parse(capture.redactedJson);
|
|
372
|
+
const records: OpenCodeObservation[] = data.records;
|
|
373
|
+
if (
|
|
374
|
+
!Array.isArray(records) ||
|
|
375
|
+
typeof data.sessionId !== "string" ||
|
|
376
|
+
!Number.isInteger(data.attempt) ||
|
|
377
|
+
data.attempt < 1
|
|
378
|
+
)
|
|
379
|
+
return "inconclusive";
|
|
380
|
+
const identities = records.find((r) => r.kind === "session_identity")
|
|
381
|
+
?.data as
|
|
382
|
+
| { sessionId?: string; userMessageId?: string; cwd?: string }
|
|
383
|
+
| undefined;
|
|
384
|
+
const requests = records
|
|
385
|
+
.filter((r) => r.kind === "http_request")
|
|
386
|
+
.map((r) => r.data as { requestId: number; url: string; method: string });
|
|
387
|
+
const promptIds = requests
|
|
388
|
+
.filter(
|
|
389
|
+
(r) =>
|
|
390
|
+
r.method === "POST" &&
|
|
391
|
+
/\/session\/[^/]+\/message(?:\?|$)/.test(r.url),
|
|
392
|
+
)
|
|
393
|
+
.map((r) => r.requestId);
|
|
394
|
+
if (
|
|
395
|
+
records.some(
|
|
396
|
+
(r) =>
|
|
397
|
+
r.kind === "http_response" &&
|
|
398
|
+
promptIds.includes((r.data as { requestId: number }).requestId) &&
|
|
399
|
+
(r.data as { status: number }).status >= 400,
|
|
400
|
+
)
|
|
401
|
+
)
|
|
402
|
+
return "external_rejection";
|
|
403
|
+
if (
|
|
404
|
+
!identities ||
|
|
405
|
+
typeof identities.sessionId !== "string" ||
|
|
406
|
+
!identities.sessionId ||
|
|
407
|
+
typeof identities.userMessageId !== "string" ||
|
|
408
|
+
!identities.userMessageId ||
|
|
409
|
+
typeof identities.cwd !== "string" ||
|
|
410
|
+
!identities.cwd
|
|
411
|
+
)
|
|
412
|
+
return "inconclusive";
|
|
413
|
+
const readback = records.filter((r) => r.kind === "readback").at(-1)
|
|
414
|
+
?.data as
|
|
415
|
+
| {
|
|
416
|
+
sessionId?: string;
|
|
417
|
+
cwd?: string;
|
|
418
|
+
coverage?: string;
|
|
419
|
+
messages?: unknown[];
|
|
420
|
+
}
|
|
421
|
+
| undefined;
|
|
422
|
+
if (
|
|
423
|
+
readback?.coverage !== "complete" ||
|
|
424
|
+
readback.sessionId !== identities.sessionId ||
|
|
425
|
+
readback.cwd !== identities.cwd ||
|
|
426
|
+
!Array.isArray(readback.messages)
|
|
427
|
+
)
|
|
428
|
+
return "inconclusive";
|
|
429
|
+
if (
|
|
430
|
+
records.some(
|
|
431
|
+
(r) =>
|
|
432
|
+
r.kind === "event" &&
|
|
433
|
+
(r.data as { type?: string; properties?: { sessionID?: string } })
|
|
434
|
+
?.type === "session.error" &&
|
|
435
|
+
(r.data as { properties?: { sessionID?: string } }).properties
|
|
436
|
+
?.sessionID === identities.sessionId,
|
|
437
|
+
)
|
|
438
|
+
)
|
|
439
|
+
return "inconclusive";
|
|
440
|
+
const messageIds = new Set<string>();
|
|
441
|
+
const partIds = new Set<string>();
|
|
442
|
+
const assistants: CapturedMessage[] = [];
|
|
443
|
+
for (const item of readback.messages) {
|
|
444
|
+
if (!item || typeof item !== "object") return "inconclusive";
|
|
445
|
+
const row = item as CapturedMessage;
|
|
446
|
+
if (
|
|
447
|
+
typeof row.info?.id !== "string" ||
|
|
448
|
+
!row.info.id ||
|
|
449
|
+
messageIds.has(row.info.id) ||
|
|
450
|
+
row.info.sessionID !== identities.sessionId
|
|
451
|
+
)
|
|
452
|
+
return "inconclusive";
|
|
453
|
+
messageIds.add(row.info.id);
|
|
454
|
+
if (row.info.role !== "user" && row.info.role !== "assistant")
|
|
455
|
+
return "inconclusive";
|
|
456
|
+
if (row.info.role !== "assistant") continue;
|
|
457
|
+
if (
|
|
458
|
+
row.info.parentID !== identities.userMessageId ||
|
|
459
|
+
row.info.path?.cwd !== identities.cwd ||
|
|
460
|
+
!Array.isArray(row.parts) ||
|
|
461
|
+
row.info.error
|
|
462
|
+
)
|
|
463
|
+
return "inconclusive";
|
|
464
|
+
for (const part of row.parts) {
|
|
465
|
+
if (
|
|
466
|
+
typeof part.id !== "string" ||
|
|
467
|
+
!part.id ||
|
|
468
|
+
partIds.has(part.id) ||
|
|
469
|
+
(part.type === "text" && typeof part.text !== "string") ||
|
|
470
|
+
part.messageID !== row.info.id ||
|
|
471
|
+
part.sessionID !== identities.sessionId ||
|
|
472
|
+
(part.type === "tool" &&
|
|
473
|
+
!["completed", "error"].includes(part.state?.status ?? ""))
|
|
474
|
+
)
|
|
475
|
+
return "inconclusive";
|
|
476
|
+
partIds.add(part.id);
|
|
477
|
+
}
|
|
478
|
+
assistants.push(row);
|
|
479
|
+
}
|
|
480
|
+
const final = assistants.at(-1);
|
|
481
|
+
if (
|
|
482
|
+
final?.info.finish !== "stop" ||
|
|
483
|
+
!Number.isFinite(final.info.time?.completed)
|
|
484
|
+
)
|
|
485
|
+
return "inconclusive";
|
|
486
|
+
const text = final.parts
|
|
487
|
+
.filter(
|
|
488
|
+
(p) => p.type === "text" && p.synthetic !== true && p.ignored !== true,
|
|
489
|
+
)
|
|
490
|
+
.map((p) => (typeof p.text === "string" ? p.text : ""))
|
|
491
|
+
.join("");
|
|
492
|
+
if (!text.trim()) return "persisted_final_empty";
|
|
493
|
+
if (text !== String(redactEvidence(localText)))
|
|
494
|
+
return "demonstrated_reconstruction_defect";
|
|
495
|
+
return "inconclusive";
|
|
496
|
+
} catch {
|
|
497
|
+
return "inconclusive";
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/** Qualification additionally requires actual wire capture and the arbiter's same final identity. */
|
|
502
|
+
export function hasCapturedTerminal(
|
|
503
|
+
capture: DiagnosticEvidence,
|
|
504
|
+
eventId: string,
|
|
505
|
+
observedAt: string,
|
|
506
|
+
): boolean {
|
|
507
|
+
const classification = classifyObservationEvidence(capture, "");
|
|
508
|
+
if (
|
|
509
|
+
classification !== "persisted_final_empty" &&
|
|
510
|
+
classification !== "demonstrated_reconstruction_defect"
|
|
511
|
+
)
|
|
512
|
+
return false;
|
|
513
|
+
try {
|
|
514
|
+
const value = JSON.parse(capture.redactedJson) as {
|
|
515
|
+
records: OpenCodeObservation[];
|
|
516
|
+
};
|
|
517
|
+
const identity = value.records.find((r) => r.kind === "session_identity")
|
|
518
|
+
?.data as { sessionId: string; userMessageId: string };
|
|
519
|
+
const readback = value.records.filter((r) => r.kind === "readback").at(-1)
|
|
520
|
+
?.data as { messages: CapturedMessage[] };
|
|
521
|
+
const final = readback.messages
|
|
522
|
+
.filter((m) => m.info.role === "assistant")
|
|
523
|
+
.at(-1);
|
|
524
|
+
if (
|
|
525
|
+
final?.info.id !== eventId ||
|
|
526
|
+
new Date(final.info.time?.completed ?? NaN).toISOString() !== observedAt
|
|
527
|
+
)
|
|
528
|
+
return false;
|
|
529
|
+
const request = value.records.find((r) => {
|
|
530
|
+
if (r.kind !== "http_request") return false;
|
|
531
|
+
const data = r.data as { url: string; method: string };
|
|
532
|
+
return (
|
|
533
|
+
data.method === "POST" &&
|
|
534
|
+
new URL(data.url).pathname === `/session/${identity.sessionId}/message`
|
|
535
|
+
);
|
|
536
|
+
})?.data as { requestId: number } | undefined;
|
|
537
|
+
return (
|
|
538
|
+
!!request &&
|
|
539
|
+
value.records.some(
|
|
540
|
+
(r) =>
|
|
541
|
+
r.kind === "http_response" &&
|
|
542
|
+
(r.data as { requestId: number }).requestId === request.requestId,
|
|
543
|
+
) &&
|
|
544
|
+
value.records.some(
|
|
545
|
+
(r) =>
|
|
546
|
+
r.kind === "request_body" &&
|
|
547
|
+
(r.data as { requestId: number; body?: { messageID?: string } })
|
|
548
|
+
.requestId === request.requestId &&
|
|
549
|
+
(r.data as { body?: { messageID?: string } }).body?.messageID ===
|
|
550
|
+
identity.userMessageId,
|
|
551
|
+
) &&
|
|
552
|
+
value.records.some((r) => r.kind === "subscription_ready")
|
|
553
|
+
);
|
|
554
|
+
} catch {
|
|
555
|
+
return false;
|
|
556
|
+
}
|
|
557
|
+
}
|