main
1// ProbeBench: the one SQLite Durable Object behind /bench. It admits a run (one at a time, at most RUNS_PER_HOUR),
2// runs it (src/bench/run.ts) against the probe namespace, keeps the record in SQLite and the exchange bodies in R2,
3// and sweeps any repository a run left behind (an alarm: revoke every token, delete, until none is left). Reached
4// through ctx.exports.ProbeBench (no env binding), like RateCounter.
5import { DurableObject } from 'cloudflare:workers';
6import type { EndedSpan } from 'ply';
7import { plyTracer } from '../edge-trace.ts';
8import { renderRay } from '../rays.ts';
9import { PLY_SHA } from '../version.ts';
10import { mintRunId, parseRunId, parseRunSeed, repoNameFor, type RunId } from './brand.ts';
11import { findScenario } from './catalogue.ts';
12import { runLive, type ProbeRepos } from './run.ts';
13import { admit, admittedSince, cleanupCounts, listRuns, loadRun, migrate, repoCleaned, repoCreated, saveRun, uncleaned, type RunSummary, type SpanRow, type Sql, type StoredRun } from './store.ts';
15export const RUNS_PER_HOUR = 20;
16const HOUR_MS = 3_600_000;
17const SWEEP_AFTER_MS = 15 * 60_000;
18const BODY_CAP = 1024 * 1024;
19const MAX_SPANS = 600;
21export type BlobBucket = {
22 put(key: string, value: Uint8Array): Promise<unknown>;
23 get(key: string): Promise<{ arrayBuffer(): Promise<ArrayBuffer> } | null>;
24};
25export type BenchEnv = { readonly PROBE_REPOS: ProbeRepos; readonly BENCH_BLOBS: BlobBucket };
27export type StartAnswer =
28 | { readonly tag: 'done'; readonly run: RunId }
29 | { readonly tag: 'refused'; readonly status: 400 | 404 | 409 | 429; readonly reason: string };
30export type Counts = { readonly created: number; readonly cleaned: number; readonly lastHour: number };
32export const bodyKey = (run: RunId, i: number, which: 'request' | 'response'): string => `runs/${run}/${i}.${which}`;
34const spanRow = (s: EndedSpan, t0: bigint): SpanRow => ({
35 id: s.spanId, parent: s.parentSpanId, name: s.name, startMs: Number((BigInt(s.startTimeUnixNano) - t0) / 1_000_000n), durationMs: s.durationMs, ok: s.status.code !== 2,
36 attrs: Object.fromEntries(Object.entries(s.attrs).filter((e): e is [string, string | number | boolean] => ['string', 'number', 'boolean'].includes(typeof e[1]))),
37});
39export class ProbeBench extends DurableObject<BenchEnv> {
40 #running: RunId | null = null;
42 #migrated = false;
44 #sql(): Sql {
45 const sql = this.ctx.storage.sql;
46 const out: Sql = { exec: (q, ...b) => sql.exec(q, ...b) };
47 if (!this.#migrated) { migrate(out); this.#migrated = true; }
48 return out;
49 }
51 async start(scenarioId: string, seedText: string): Promise<StartAnswer> {
52 const entry = findScenario(scenarioId);
53 if (entry.tag === 'none') return { tag: 'refused', status: 404, reason: `no scenario ${scenarioId} in ply's catalogue at ${PLY_SHA}` };
54 const support = entry.value.support;
55 if (support.tag === 'git-only') return { tag: 'refused', status: 400, reason: `stock git only: ${support.why}` };
56 if (support.tag === 'too-large') return { tag: 'refused', status: 400, reason: `the seed is ${support.bytes} bytes; the workbench holds at most 8 MiB in a Worker` };
57 const seed = parseRunSeed(seedText);
58 if (!seed.ok) return { tag: 'refused', status: 400, reason: seed.error };
59 if (this.#running !== null) return { tag: 'refused', status: 409, reason: `run ${this.#running} is in progress; one run at a time` };
60 const sql = this.#sql();
61 const now = Date.now();
62 if (admittedSince(sql, now - HOUR_MS) >= RUNS_PER_HOUR) return { tag: 'refused', status: 429, reason: `at most ${RUNS_PER_HOUR} runs an hour` };
63 const id = mintRunId();
64 const repo = repoNameFor(seed.value, id);
65 this.#running = id;
66 admit(sql, id, now);
67 await this.ctx.storage.setAlarm(now + SWEEP_AFTER_MS);
68 const ended: EndedSpan[] = [];
69 const tracer = plyTracer(this.ctx, { onEnd: s => { if (ended.length < MAX_SPANS) ended.push(s); }, resource: { 'service.name': 'ply-bench' } });
70 const puts: Array<Promise<unknown>> = [];
71 const keep = (i: number, request: Uint8Array, response: Uint8Array): void => {
72 if (request.length > 0) puts.push(this.env.BENCH_BLOBS.put(bodyKey(id, i, 'request'), request.subarray(0, BODY_CAP)).catch(() => null));
73 if (response.length > 0) puts.push(this.env.BENCH_BLOBS.put(bodyKey(id, i, 'response'), response.subarray(0, BODY_CAP)).catch(() => null));
74 };
75 try {
76 const result = await runLive({ repos: this.env.PROBE_REPOS, upstream: async (input, init) => fetch(input, init), tracer, parent: null, keepBodies: keep, created: async name => { repoCreated(sql, name, id, Date.now()); } }, entry.value.scenario, seed.value, repo);
77 await Promise.all(puts);
78 const c = result.cleanup;
79 if (c.refused && c.deleted && c.gone) repoCleaned(sql, repo, Date.now());
80 const t0 = BigInt(now) * 1_000_000n;
81 const record: StoredRun = { id, scenario: entry.value.scenario.id, seed: seed.value, repo, startedAt: now, finishedAt: Date.now(), ply: PLY_SHA, result, spans: ended.map(s => spanRow(s, t0)) };
82 saveRun(sql, record);
83 console.log(JSON.stringify({ msg: 'ply.bench.run', run: id, scenario: record.scenario, verdict: result.verdict.tag, exchanges: result.exchanges.length, cf_rays: result.exchanges.map(x => renderRay(x.cfRay)), tokens_listed: c.tokensListed, tokens_revoked: c.tokensRevoked, revoked_status: c.revokedStatus, deleted: c.deleted, gone: c.gone, ms: record.finishedAt - now }));
84 return { tag: 'done', run: id };
85 } finally {
86 this.#running = null;
87 }
88 }
90 async runs(limit: number): Promise<RunSummary[]> { return listRuns(this.#sql(), Math.min(200, Math.max(1, limit))); }
91 async run(id: string): Promise<StoredRun | null> {
92 const r = parseRunId(id);
93 return r.ok ? loadRun(this.#sql(), r.value) : null;
94 }
95 async body(id: string, i: number, which: 'request' | 'response'): Promise<Uint8Array | null> {
96 const r = parseRunId(id);
97 if (!r.ok || !Number.isSafeInteger(i) || i < 0) return null;
98 const o = await this.env.BENCH_BLOBS.get(bodyKey(r.value, i, which));
99 return o === null ? null : new Uint8Array(await o.arrayBuffer());
100 }
101 async counts(): Promise<Counts> {
102 const sql = this.#sql();
103 return { ...cleanupCounts(sql), lastHour: admittedSince(sql, Date.now() - HOUR_MS) };
104 }
106 // The sweep: any repository a run created and did not prove cleaned. Revoke every token, delete, record it.
107 async alarm(): Promise<void> {
108 const sql = this.#sql();
109 const left = uncleaned(sql, Date.now() - (this.#running === null ? 0 : SWEEP_AFTER_MS));
110 let swept = 0;
111 for (const name of left) {
112 let ok = false;
113 try {
114 const repo = await this.env.PROBE_REPOS.get(name);
115 for (const t of (await repo.listTokens()).tokens) if (t.state === 'active') await repo.revokeToken(t.id);
116 await this.env.PROBE_REPOS.delete(name);
117 ok = true;
118 } catch (e) {
119 // a repository that is already gone is cleaned
120 ok = /NOT_FOUND|not found/i.test(e instanceof Error ? `${e.name} ${e.message}` : String(e));
121 }
122 if (ok) { repoCleaned(sql, name as never, Date.now()); swept++; }
123 }
124 console.log(JSON.stringify({ msg: 'ply.bench.sweep', left: left.length, swept }));
125 if (uncleaned(sql, Number.MAX_SAFE_INTEGER).length > 0) await this.ctx.storage.setAlarm(Date.now() + SWEEP_AFTER_MS);
126 }
127}
Diagnostics4 git calls · 298 ms
CallStatusSizeTime0298 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200145 B · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=145 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=145 git.pkt.payload.max=76 git.ls_refs.count=2 git.ls_refs.head=refs/heads/main
fetchblob:none · deepen 1020040 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=41349 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=526b305930b8ef382f2d7a1f9c0238730a585921 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=6 git.pkt.data=4 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=41349 git.pkt.payload.max=41254 git.sideband.band1.bytes=41253 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=61 git.pack.bytes=41253 git.pack.ofs_delta=0 git.pack.ref_delta=0 git.pack.chain.max=0 git.pack.trailer=ab60efef4b7243678c75de64a4386986ee885c1c git.pack.full=61Open pack map
fetch2002.9 KiB292 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=292 git.response.bytes=2923 git.response.cache=MISS git.response.duration_ms=292 git.fetch.wants=01a9ee8dd3222fe77f68d919d7578efdde62b1db git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=2923 git.pkt.payload.max=2902 git.sideband.band1.bytes=2901 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/ply-web.git
- Ref
- refs/heads/main
- Commit
- 526b305930b8ef382f2d7a1f9c0238730a585921
- cf-ray
- a5e2e41049e52947
- Request
- a37eeb02…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format