crysk 0.0.0 → 0.3.1
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 +611 -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/SKILL-y8rv8msh.md +134 -0
- package/dist/SKILL-ygka1bs0.md +515 -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/ci-admission-qk9r9taq.md +124 -0
- package/dist/cli.js +42501 -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 +124 -0
- package/skills/crysk-ci-setup/references/opencode-ci.md +141 -0
- package/skills/crysk-loop/SKILL.md +515 -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 +945 -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 +910 -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 +984 -0
- package/src/pr/answered-findings.ts +169 -0
- package/src/pr/ci-admission-gate.ts +743 -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 +845 -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,229 @@
|
|
|
1
|
+
// The observability store (W4 / GitHub #23), pure half: the v1 schema DDL
|
|
2
|
+
// and the projection from a completed run's already-on-disk facts
|
|
3
|
+
// (FindingsDocument, per-agent usage) into the
|
|
4
|
+
// flat rows store/metrics.ts's bun:sqlite shell writes. Artifacts on disk stay the
|
|
5
|
+
// source of truth — this module only reshapes what already exists into rows,
|
|
6
|
+
// it never reads a file or opens a connection.
|
|
7
|
+
//
|
|
8
|
+
// No backfill (proposal, decided): the dataset starts empty and grows one
|
|
9
|
+
// ingest at a time, from the very next completed review after ship.
|
|
10
|
+
|
|
11
|
+
import type { FindingsDocument, RunStatus } from "#review/findings";
|
|
12
|
+
import type { PerAgentUsage } from "#review/pipeline";
|
|
13
|
+
|
|
14
|
+
export const CURRENT_SCHEMA_VERSION = 1;
|
|
15
|
+
|
|
16
|
+
export interface SchemaMigration {
|
|
17
|
+
toVersion: number;
|
|
18
|
+
statements: string[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// `runs.run_dir` is the unique run key (design decision table): a run that
|
|
22
|
+
// cost money never collides, and re-ingesting the same run_dir upserts the
|
|
23
|
+
// parent row instead of duplicating it. `repo_id` is a column, never part of
|
|
24
|
+
// a path — two checkouts of the same origin SHARE it, the same way they
|
|
25
|
+
// share worktrees (W3). `pr` is nullable SQL, not a sentinel 0: the document
|
|
26
|
+
// already stamps local runs `pr: 0` (schema 1.0.0, unchanged), and the
|
|
27
|
+
// ingest layer maps that to NULL — the real "not a PR" value.
|
|
28
|
+
const V1_STATEMENTS: string[] = [
|
|
29
|
+
`CREATE TABLE IF NOT EXISTS runs (
|
|
30
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
31
|
+
repo_id TEXT NOT NULL,
|
|
32
|
+
run_dir TEXT NOT NULL UNIQUE,
|
|
33
|
+
pr INTEGER NULL,
|
|
34
|
+
checkout_path TEXT NULL,
|
|
35
|
+
head_sha TEXT NOT NULL,
|
|
36
|
+
base_sha TEXT NOT NULL,
|
|
37
|
+
run_status TEXT NOT NULL,
|
|
38
|
+
session_failed INTEGER NOT NULL,
|
|
39
|
+
model TEXT NOT NULL,
|
|
40
|
+
generated_at TEXT NOT NULL,
|
|
41
|
+
wall_ms INTEGER NOT NULL,
|
|
42
|
+
index_ms INTEGER NOT NULL,
|
|
43
|
+
tokens_in INTEGER NOT NULL,
|
|
44
|
+
tokens_out INTEGER NOT NULL,
|
|
45
|
+
tokens_total INTEGER NOT NULL,
|
|
46
|
+
cost_usd_est REAL NOT NULL,
|
|
47
|
+
blocking INTEGER NOT NULL,
|
|
48
|
+
advisory INTEGER NOT NULL
|
|
49
|
+
)`,
|
|
50
|
+
`CREATE INDEX IF NOT EXISTS idx_runs_repo_id ON runs (repo_id)`,
|
|
51
|
+
// Child tables key off the PARENT ROWID, not run_dir directly: a re-ingest
|
|
52
|
+
// deletes and reinserts these by run_id so a double ingest of the same
|
|
53
|
+
// run_dir never duplicates child rows (spec "Idempotent Run Identity").
|
|
54
|
+
`CREATE TABLE IF NOT EXISTS run_agents (
|
|
55
|
+
run_id INTEGER NOT NULL REFERENCES runs (id) ON DELETE CASCADE,
|
|
56
|
+
agent_key TEXT NOT NULL,
|
|
57
|
+
tokens_total INTEGER NOT NULL,
|
|
58
|
+
duration_ms INTEGER NOT NULL,
|
|
59
|
+
tokens_in INTEGER NOT NULL,
|
|
60
|
+
tokens_out INTEGER NOT NULL,
|
|
61
|
+
cost_usd_est REAL NOT NULL,
|
|
62
|
+
attempts INTEGER NOT NULL,
|
|
63
|
+
status TEXT NOT NULL,
|
|
64
|
+
PRIMARY KEY (run_id, agent_key)
|
|
65
|
+
)`,
|
|
66
|
+
`CREATE TABLE IF NOT EXISTS comparison_rows (
|
|
67
|
+
run_id INTEGER NOT NULL REFERENCES runs (id) ON DELETE CASCADE,
|
|
68
|
+
row_index INTEGER NOT NULL,
|
|
69
|
+
bucket TEXT NOT NULL,
|
|
70
|
+
greptile_json TEXT NULL,
|
|
71
|
+
prhero_json TEXT NULL,
|
|
72
|
+
verdict TEXT NULL,
|
|
73
|
+
reasoning TEXT NULL,
|
|
74
|
+
actor TEXT NULL,
|
|
75
|
+
PRIMARY KEY (run_id, row_index)
|
|
76
|
+
)`,
|
|
77
|
+
];
|
|
78
|
+
|
|
79
|
+
// PRAGMA user_version-based versioning (design decision table, over a
|
|
80
|
+
// version table): the shell reads the current value, asks this pure
|
|
81
|
+
// function what to run, executes the statements, then stamps the pragma.
|
|
82
|
+
// Additive only for now — v1 is the whole schema this PR ships.
|
|
83
|
+
export function migrationsFor(currentVersion: number): SchemaMigration {
|
|
84
|
+
if (currentVersion >= CURRENT_SCHEMA_VERSION) {
|
|
85
|
+
return { toVersion: currentVersion, statements: [] };
|
|
86
|
+
}
|
|
87
|
+
return { toVersion: CURRENT_SCHEMA_VERSION, statements: [...V1_STATEMENTS] };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export interface RunRow {
|
|
91
|
+
repo_id: string;
|
|
92
|
+
run_dir: string;
|
|
93
|
+
pr: number | null;
|
|
94
|
+
checkout_path: string | null;
|
|
95
|
+
head_sha: string;
|
|
96
|
+
base_sha: string;
|
|
97
|
+
run_status: RunStatus;
|
|
98
|
+
session_failed: 0 | 1;
|
|
99
|
+
model: string;
|
|
100
|
+
generated_at: string;
|
|
101
|
+
wall_ms: number;
|
|
102
|
+
index_ms: number;
|
|
103
|
+
tokens_in: number;
|
|
104
|
+
tokens_out: number;
|
|
105
|
+
tokens_total: number;
|
|
106
|
+
cost_usd_est: number;
|
|
107
|
+
blocking: number;
|
|
108
|
+
advisory: number;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export interface RunAgentRow {
|
|
112
|
+
agent_key: string;
|
|
113
|
+
tokens_total: number;
|
|
114
|
+
duration_ms: number;
|
|
115
|
+
tokens_in: number;
|
|
116
|
+
tokens_out: number;
|
|
117
|
+
cost_usd_est: number;
|
|
118
|
+
attempts: number;
|
|
119
|
+
status: string;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export interface ProjectedRun {
|
|
123
|
+
run: RunRow;
|
|
124
|
+
agents: RunAgentRow[];
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// The ONLY place a completed run's on-disk facts become sqlite rows. Pure:
|
|
128
|
+
// the caller (store/metrics.ts's ingestRun) supplies repoId (from the SAME
|
|
129
|
+
// resolveRepoHome call the run dir already paid for — never re-derived from
|
|
130
|
+
// checkoutPath, which is diagnostic metadata only) and generatedAt (the
|
|
131
|
+
// shell's clock). `doc.pr === 0` is schema 1.0.0's "not a PR" sentinel
|
|
132
|
+
// (review/findings.ts, unchanged) — this is the one place it becomes real SQL NULL.
|
|
133
|
+
export function projectRunRow(input: {
|
|
134
|
+
doc: FindingsDocument;
|
|
135
|
+
perAgent: Record<string, PerAgentUsage>;
|
|
136
|
+
repoId: string;
|
|
137
|
+
runDir: string;
|
|
138
|
+
checkoutPath: string | null;
|
|
139
|
+
generatedAt: string;
|
|
140
|
+
}): ProjectedRun {
|
|
141
|
+
let blocking = 0;
|
|
142
|
+
let advisory = 0;
|
|
143
|
+
for (const finding of input.doc.findings) {
|
|
144
|
+
if (finding.tier === "blocking") blocking++;
|
|
145
|
+
else advisory++;
|
|
146
|
+
}
|
|
147
|
+
const run: RunRow = {
|
|
148
|
+
repo_id: input.repoId,
|
|
149
|
+
run_dir: input.runDir,
|
|
150
|
+
pr: input.doc.pr === 0 ? null : input.doc.pr,
|
|
151
|
+
checkout_path: input.checkoutPath,
|
|
152
|
+
head_sha: input.doc.head_sha,
|
|
153
|
+
base_sha: input.doc.base_sha,
|
|
154
|
+
run_status: input.doc.run_status,
|
|
155
|
+
session_failed: input.doc.sessionFailed ? 1 : 0,
|
|
156
|
+
model: input.doc.model,
|
|
157
|
+
generated_at: input.generatedAt,
|
|
158
|
+
wall_ms: input.doc.telemetry.wall_ms,
|
|
159
|
+
index_ms: input.doc.telemetry.index_ms,
|
|
160
|
+
tokens_in: input.doc.telemetry.tokens_in,
|
|
161
|
+
tokens_out: input.doc.telemetry.tokens_out,
|
|
162
|
+
tokens_total: input.doc.telemetry.tokens_total,
|
|
163
|
+
cost_usd_est: input.doc.telemetry.cost_usd_est,
|
|
164
|
+
blocking,
|
|
165
|
+
advisory,
|
|
166
|
+
};
|
|
167
|
+
const agents: RunAgentRow[] = Object.entries(input.perAgent).map(
|
|
168
|
+
([agentKey, usage]) => ({
|
|
169
|
+
agent_key: agentKey,
|
|
170
|
+
tokens_total: usage.tokens_total,
|
|
171
|
+
duration_ms: usage.duration_ms,
|
|
172
|
+
tokens_in: usage.tokens_in,
|
|
173
|
+
tokens_out: usage.tokens_out,
|
|
174
|
+
cost_usd_est: usage.cost_usd_est,
|
|
175
|
+
attempts: usage.attempts,
|
|
176
|
+
status: usage.status,
|
|
177
|
+
}),
|
|
178
|
+
);
|
|
179
|
+
return { run, agents };
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export interface RenderUsageOptions {
|
|
183
|
+
// Accepted for the same reason every other renderer in this codebase
|
|
184
|
+
// (ui/result.ts, ui/tree.ts) takes a styles flag — a uniform signature
|
|
185
|
+
// across renderers, so a caller never special-cases one of them. Usage
|
|
186
|
+
// is a PIPEABLE REPORT (design decision table: "not ui-*.ts painted"):
|
|
187
|
+
// this module never imports ui.ts's ANSI helpers, so there is nothing to
|
|
188
|
+
// turn off — the output is unconditionally free of escape bytes.
|
|
189
|
+
styles: boolean;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// `pr-hero usage`'s render, pure (spec "Plain-Text Rendering"): a markdown
|
|
193
|
+
// table (one line per run, `pr` mapped back to "local" for the NULL rows
|
|
194
|
+
// projectRunRow produced) plus a totals footer that is a real reduction
|
|
195
|
+
// over the input, never a placeholder — cli.ts prints these lines straight
|
|
196
|
+
// to stdout (human notes on stderr via log(), the report itself on stdout).
|
|
197
|
+
export function renderUsage(
|
|
198
|
+
rows: RunRow[],
|
|
199
|
+
_options: RenderUsageOptions,
|
|
200
|
+
): string[] {
|
|
201
|
+
const out: string[] = [];
|
|
202
|
+
out.push(
|
|
203
|
+
"| generated_at | repo_id | pr | status | blocking | advisory | " +
|
|
204
|
+
"tokens | cost | run_dir |",
|
|
205
|
+
);
|
|
206
|
+
out.push("| --- | --- | --- | --- | --- | --- | --- | --- | --- |");
|
|
207
|
+
for (const row of rows) {
|
|
208
|
+
const status =
|
|
209
|
+
row.run_status + (row.session_failed ? " (session failed)" : "");
|
|
210
|
+
out.push(
|
|
211
|
+
`| ${row.generated_at} | ${row.repo_id} | ${row.pr ?? "local"} | ` +
|
|
212
|
+
`${status} | ${row.blocking} | ${row.advisory} | ` +
|
|
213
|
+
`${row.tokens_total} | $${row.cost_usd_est.toFixed(2)} | ` +
|
|
214
|
+
`${row.run_dir} |`,
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
let totalCost = 0;
|
|
218
|
+
let totalTokens = 0;
|
|
219
|
+
for (const row of rows) {
|
|
220
|
+
totalCost += row.cost_usd_est;
|
|
221
|
+
totalTokens += row.tokens_total;
|
|
222
|
+
}
|
|
223
|
+
out.push("");
|
|
224
|
+
out.push(
|
|
225
|
+
`${rows.length} run(s) — $${totalCost.toFixed(2)} total, ` +
|
|
226
|
+
`${totalTokens} tokens.`,
|
|
227
|
+
);
|
|
228
|
+
return out;
|
|
229
|
+
}
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
// The observability store (W4 / GitHub #23), impure half: the bun:sqlite
|
|
2
|
+
// file handle, the migration runner, the one-transaction ingest, the
|
|
3
|
+
// origin-scoped/--all read, and the fail-soft wrapper cli.ts calls after
|
|
4
|
+
// every completed review (local and PR). Everything decidable without a
|
|
5
|
+
// file handle lives in store/metrics-preflight.ts; this module only opens the db,
|
|
6
|
+
// runs statements, and closes it.
|
|
7
|
+
//
|
|
8
|
+
// Fail-soft is the load-bearing contract here (proposal risk table): a
|
|
9
|
+
// sqlite write must NEVER fail a paid review. `ingestRun` is allowed to
|
|
10
|
+
// throw — `failSoftIngest` is the ONLY caller cli.ts uses, and it is the
|
|
11
|
+
// one place that turns a throw into a warning.
|
|
12
|
+
|
|
13
|
+
import { Database, type SQLQueryBindings } from "bun:sqlite";
|
|
14
|
+
import { mkdirSync } from "node:fs";
|
|
15
|
+
import path from "node:path";
|
|
16
|
+
import type { FindingsDocument } from "#review/findings";
|
|
17
|
+
import type { PerAgentUsage } from "#review/pipeline";
|
|
18
|
+
import {
|
|
19
|
+
migrationsFor,
|
|
20
|
+
projectRunRow,
|
|
21
|
+
type RunAgentRow,
|
|
22
|
+
type RunRow,
|
|
23
|
+
} from "./metrics-preflight";
|
|
24
|
+
|
|
25
|
+
// WAL + a 5s busy_timeout (design decision table, #35 concurrency out of
|
|
26
|
+
// scope): two reviews of two different repos never collide on one process,
|
|
27
|
+
// and a reader (`pr-hero usage`) never blocks a writer past a few ms.
|
|
28
|
+
const BUSY_TIMEOUT_MS = 5000;
|
|
29
|
+
|
|
30
|
+
export function openMetricsDb(dbPath: string): Database {
|
|
31
|
+
mkdirSync(path.dirname(dbPath), { recursive: true });
|
|
32
|
+
const db = new Database(dbPath, { create: true });
|
|
33
|
+
db.exec("PRAGMA journal_mode = WAL;");
|
|
34
|
+
db.exec(`PRAGMA busy_timeout = ${BUSY_TIMEOUT_MS};`);
|
|
35
|
+
const current = (
|
|
36
|
+
db.query("PRAGMA user_version;").get() as { user_version: number }
|
|
37
|
+
).user_version;
|
|
38
|
+
const migration = migrationsFor(current);
|
|
39
|
+
if (migration.statements.length > 0) {
|
|
40
|
+
db.transaction(() => {
|
|
41
|
+
for (const statement of migration.statements) {
|
|
42
|
+
db.exec(statement);
|
|
43
|
+
}
|
|
44
|
+
})();
|
|
45
|
+
// PRAGMA cannot bind a parameter — the value comes from our own
|
|
46
|
+
// pure migrationsFor, never from caller input, so interpolation here
|
|
47
|
+
// is not an injection surface.
|
|
48
|
+
db.exec(`PRAGMA user_version = ${migration.toVersion};`);
|
|
49
|
+
}
|
|
50
|
+
return db;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface IngestRunInput {
|
|
54
|
+
dbPath: string;
|
|
55
|
+
repoId: string;
|
|
56
|
+
runDir: string;
|
|
57
|
+
checkoutPath: string | null;
|
|
58
|
+
doc: FindingsDocument;
|
|
59
|
+
perAgent: Record<string, PerAgentUsage>;
|
|
60
|
+
generatedAt?: string;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const RUN_UPSERT_SQL = `
|
|
64
|
+
INSERT INTO runs (
|
|
65
|
+
repo_id, run_dir, pr, checkout_path, head_sha, base_sha, run_status,
|
|
66
|
+
session_failed, model, generated_at, wall_ms, index_ms, tokens_in,
|
|
67
|
+
tokens_out, tokens_total, cost_usd_est, blocking, advisory
|
|
68
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
69
|
+
ON CONFLICT(run_dir) DO UPDATE SET
|
|
70
|
+
repo_id = excluded.repo_id,
|
|
71
|
+
pr = excluded.pr,
|
|
72
|
+
checkout_path = excluded.checkout_path,
|
|
73
|
+
head_sha = excluded.head_sha,
|
|
74
|
+
base_sha = excluded.base_sha,
|
|
75
|
+
run_status = excluded.run_status,
|
|
76
|
+
session_failed = excluded.session_failed,
|
|
77
|
+
model = excluded.model,
|
|
78
|
+
generated_at = excluded.generated_at,
|
|
79
|
+
wall_ms = excluded.wall_ms,
|
|
80
|
+
index_ms = excluded.index_ms,
|
|
81
|
+
tokens_in = excluded.tokens_in,
|
|
82
|
+
tokens_out = excluded.tokens_out,
|
|
83
|
+
tokens_total = excluded.tokens_total,
|
|
84
|
+
cost_usd_est = excluded.cost_usd_est,
|
|
85
|
+
blocking = excluded.blocking,
|
|
86
|
+
advisory = excluded.advisory
|
|
87
|
+
`;
|
|
88
|
+
|
|
89
|
+
function runParams(row: RunRow): SQLQueryBindings[] {
|
|
90
|
+
return [
|
|
91
|
+
row.repo_id,
|
|
92
|
+
row.run_dir,
|
|
93
|
+
row.pr,
|
|
94
|
+
row.checkout_path,
|
|
95
|
+
row.head_sha,
|
|
96
|
+
row.base_sha,
|
|
97
|
+
row.run_status,
|
|
98
|
+
row.session_failed,
|
|
99
|
+
row.model,
|
|
100
|
+
row.generated_at,
|
|
101
|
+
row.wall_ms,
|
|
102
|
+
row.index_ms,
|
|
103
|
+
row.tokens_in,
|
|
104
|
+
row.tokens_out,
|
|
105
|
+
row.tokens_total,
|
|
106
|
+
row.cost_usd_est,
|
|
107
|
+
row.blocking,
|
|
108
|
+
row.advisory,
|
|
109
|
+
];
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// ONE transaction: upsert the parent by run_dir (the unique run key), then
|
|
113
|
+
// DELETE + re-INSERT every child table scoped to that run's id. Delete-then-
|
|
114
|
+
// insert, never a diff, because the child sets are always small (one row
|
|
115
|
+
// per agent) and a diff buys nothing a
|
|
116
|
+
// full replace does not already give for free — spec "Idempotent Run
|
|
117
|
+
// Identity": a double ingest of the same run_dir must not duplicate rows.
|
|
118
|
+
export function ingestRun(input: IngestRunInput): void {
|
|
119
|
+
const db = openMetricsDb(input.dbPath);
|
|
120
|
+
try {
|
|
121
|
+
const projected = projectRunRow({
|
|
122
|
+
doc: input.doc,
|
|
123
|
+
perAgent: input.perAgent,
|
|
124
|
+
repoId: input.repoId,
|
|
125
|
+
runDir: input.runDir,
|
|
126
|
+
checkoutPath: input.checkoutPath,
|
|
127
|
+
generatedAt: input.generatedAt ?? new Date().toISOString(),
|
|
128
|
+
});
|
|
129
|
+
db.transaction(() => {
|
|
130
|
+
db.query(RUN_UPSERT_SQL).run(...runParams(projected.run));
|
|
131
|
+
const runRow = db
|
|
132
|
+
.query("SELECT id FROM runs WHERE run_dir = ?")
|
|
133
|
+
.get(projected.run.run_dir) as { id: number };
|
|
134
|
+
const runId = runRow.id;
|
|
135
|
+
db.query("DELETE FROM run_agents WHERE run_id = ?").run(runId);
|
|
136
|
+
insertAgents(db, runId, projected.agents);
|
|
137
|
+
})();
|
|
138
|
+
} finally {
|
|
139
|
+
db.close();
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function insertAgents(
|
|
144
|
+
db: Database,
|
|
145
|
+
runId: number,
|
|
146
|
+
agents: RunAgentRow[],
|
|
147
|
+
): void {
|
|
148
|
+
if (agents.length === 0) return;
|
|
149
|
+
const insert = db.query(`
|
|
150
|
+
INSERT INTO run_agents (
|
|
151
|
+
run_id, agent_key, tokens_total, duration_ms, tokens_in, tokens_out,
|
|
152
|
+
cost_usd_est, attempts, status
|
|
153
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
154
|
+
`);
|
|
155
|
+
for (const agent of agents) {
|
|
156
|
+
insert.run(
|
|
157
|
+
runId,
|
|
158
|
+
agent.agent_key,
|
|
159
|
+
agent.tokens_total,
|
|
160
|
+
agent.duration_ms,
|
|
161
|
+
agent.tokens_in,
|
|
162
|
+
agent.tokens_out,
|
|
163
|
+
agent.cost_usd_est,
|
|
164
|
+
agent.attempts,
|
|
165
|
+
agent.status,
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export interface UsageRow extends RunRow {}
|
|
171
|
+
|
|
172
|
+
export type UsageScope = { repoId: string } | { all: true };
|
|
173
|
+
|
|
174
|
+
// Origin-scoped by default (spec "Origin-Scoped Usage By Default"); `--all`
|
|
175
|
+
// is the operator-wide escape hatch. Reads only the parent `runs` table —
|
|
176
|
+
// PR2's renderUsage decides what, if anything, joins the child tables.
|
|
177
|
+
export function queryUsage(dbPath: string, scope: UsageScope): UsageRow[] {
|
|
178
|
+
const db = openMetricsDb(dbPath);
|
|
179
|
+
try {
|
|
180
|
+
if ("all" in scope) {
|
|
181
|
+
return db
|
|
182
|
+
.query("SELECT * FROM runs ORDER BY generated_at DESC;")
|
|
183
|
+
.all() as UsageRow[];
|
|
184
|
+
}
|
|
185
|
+
return db
|
|
186
|
+
.query("SELECT * FROM runs WHERE repo_id = ? ORDER BY generated_at DESC;")
|
|
187
|
+
.all(scope.repoId) as UsageRow[];
|
|
188
|
+
} finally {
|
|
189
|
+
db.close();
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export interface FailSoftIngestInput {
|
|
194
|
+
dbPath: string;
|
|
195
|
+
// null means no resolvable git origin for this run's checkout (W4 Phase 6
|
|
196
|
+
// remediation, GitHub #23 option D): --out no longer bypasses the origin
|
|
197
|
+
// lookup — createRunDir tries it via tryOriginRepoId, and this stays null
|
|
198
|
+
// only when that lookup itself fails (no `origin` remote, or an empty
|
|
199
|
+
// one) — the same "no resolvable origin" case every other global-state
|
|
200
|
+
// path already refuses.
|
|
201
|
+
repoId: string | null;
|
|
202
|
+
runDir: string;
|
|
203
|
+
checkoutPath: string | null;
|
|
204
|
+
doc: FindingsDocument;
|
|
205
|
+
perAgent: Record<string, PerAgentUsage>;
|
|
206
|
+
log: (line: string) => void;
|
|
207
|
+
// Test seam only — production always uses ingestRun.
|
|
208
|
+
ingest?: (input: IngestRunInput) => void;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const WARNING_PREFIX =
|
|
212
|
+
"warning: metrics ingest failed — the review itself is intact: ";
|
|
213
|
+
|
|
214
|
+
// The ONLY function cli.ts calls after writeFindings. Never
|
|
215
|
+
// throws — a sqlite failure (or a missing repo_id) degrades to a printed
|
|
216
|
+
// warning, and the review's own exit code is untouched (spec "Fail-Soft
|
|
217
|
+
// Ingest").
|
|
218
|
+
export function failSoftIngest(input: FailSoftIngestInput): void {
|
|
219
|
+
if (input.repoId === null) {
|
|
220
|
+
input.log(`${WARNING_PREFIX}no repo_id resolved for this run`);
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
const ingest = input.ingest ?? ingestRun;
|
|
224
|
+
try {
|
|
225
|
+
ingest({
|
|
226
|
+
dbPath: input.dbPath,
|
|
227
|
+
repoId: input.repoId,
|
|
228
|
+
runDir: input.runDir,
|
|
229
|
+
checkoutPath: input.checkoutPath,
|
|
230
|
+
doc: input.doc,
|
|
231
|
+
perAgent: input.perAgent,
|
|
232
|
+
});
|
|
233
|
+
} catch (error) {
|
|
234
|
+
input.log(`${WARNING_PREFIX}${(error as Error).message}`);
|
|
235
|
+
}
|
|
236
|
+
}
|