Skip to content

Commit d7a3ac1

Browse files
authored
feat(bay): chart GitHub throttle history (#1174)
* feat(bay): chart GitHub throttle history * fix(proof): validate exact GitHub API hostname * fix(telemetry): fail closed on incomplete egress * test(telemetry): format incomplete history coverage * fix(bay): label reset-header observations accurately
1 parent 61276c8 commit d7a3ac1

16 files changed

Lines changed: 970 additions & 51 deletions

‎dashboard/bay-page.ts‎

Lines changed: 24 additions & 2 deletions
Large diffs are not rendered by default.

‎dashboard/github-egress-telemetry.ts‎

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const RECEIPT_RETENTION_MS = 7 * 24 * 60 * 60 * 1000;
2424
const MAX_ROLLUP_ROWS = 50_000;
2525
const MAX_RATE_LIMIT_ROWS = 10_000;
2626
const MAX_PUBLIC_ROWS = 2_000;
27+
const MAX_PUBLIC_THROTTLE_ROWS = 7 * 24 * GITHUB_EGRESS_POOL_CLASSES.length * 2;
2728
const MAX_METRICS_PER_SUBMISSION = 128;
2829
const MAX_RATE_LIMITS_PER_SUBMISSION = 16;
2930
const MAX_RATE_LIMIT_INTEGER = 10_000_000_000;
@@ -517,11 +518,12 @@ export class GithubEgressTelemetryStore {
517518

518519
publicObservability(hours: number, now = Date.now()) {
519520
const boundedHours =
520-
hours === 0.25 || hours === 1 || hours === 6 || hours === 24 ? hours : null;
521+
hours === 0.25 || hours === 1 || hours === 6 || hours === 24 || hours === 168 ? hours : null;
521522
if (!boundedHours) return null;
522523
const bucketKind = boundedHours <= 6 ? "five_minute" : "hour";
523524
const windowStart = now - boundedHours * 60 * 60 * 1000;
524525
const bucketStart = alignedBucketStart(bucketKind, windowStart);
526+
const closedThrough = alignedBucketStart(bucketKind, now);
525527
const rows = Array.from(
526528
this.storage.sql.exec(
527529
`SELECT bucket_start, deployment_revision, config_revision, pool_class,
@@ -586,15 +588,82 @@ export class GithubEgressTelemetryStore {
586588
const completeness = this.completenessSince(bucketKind, bucketStart);
587589
const diagnostics = this.diagnostics();
588590
const rollupWindowIsComplete = rollupWindowComplete(diagnostics, bucketKind, bucketStart);
589-
const rateLimitWindowIsComplete = rateLimitWindowComplete(diagnostics, windowStart);
591+
const rateLimitDetailCoversWindow = boundedHours <= RATE_LIMIT_RETENTION_MS / 3_600_000;
592+
const rateLimitWindowIsComplete =
593+
rateLimitDetailCoversWindow && rateLimitWindowComplete(diagnostics, windowStart);
594+
const firstAvailableRows = Array.from(
595+
this.storage.sql.exec(
596+
`SELECT MIN(bucket_start) AS bucket_start
597+
FROM ${ROLLUP_TABLE}
598+
WHERE bucket_kind = ?`,
599+
bucketKind,
600+
),
601+
) as Array<Record<string, unknown>>;
602+
const firstAvailableBucket = finiteTimestamp(firstAvailableRows[0]?.bucket_start);
603+
const throttleRows = Array.from(
604+
this.storage.sql.exec(
605+
`SELECT bucket_start, pool_class, status_bucket, SUM(count) AS count
606+
FROM ${ROLLUP_TABLE}
607+
WHERE bucket_kind = ? AND bucket_start >= ? AND bucket_start < ?
608+
AND unit = 'wire_attempt' AND outcome = 'throttle'
609+
AND attempted = 1 AND telemetry_complete = 1
610+
AND status_bucket IN ('403', '429')
611+
GROUP BY bucket_start, pool_class, status_bucket
612+
ORDER BY bucket_start ASC, pool_class, status_bucket
613+
LIMIT ?`,
614+
bucketKind,
615+
bucketStart,
616+
closedThrough,
617+
MAX_PUBLIC_THROTTLE_ROWS + 1,
618+
),
619+
) as Array<Record<string, unknown>>;
620+
const throttleRowsTruncated = throttleRows.length > MAX_PUBLIC_THROTTLE_ROWS;
621+
const incompleteEgressRows = Array.from(
622+
this.storage.sql.exec(
623+
`SELECT SUM(count) AS count
624+
FROM ${ROLLUP_TABLE}
625+
WHERE bucket_kind = ? AND bucket_start >= ? AND bucket_start < ?
626+
AND telemetry_complete = 0`,
627+
bucketKind,
628+
bucketStart,
629+
closedThrough,
630+
),
631+
) as Array<Record<string, unknown>>;
632+
const excludedIncompleteEgressCount = Number(incompleteEgressRows[0]?.count || 0);
633+
// A first observation only proves coverage from somewhere inside its rollup
634+
// bucket. Require an earlier bucket before zero-filling the requested edge.
635+
const throttleCoverageComplete =
636+
firstAvailableBucket !== null && firstAvailableBucket < bucketStart;
637+
const throttleSeriesComplete =
638+
!throttleRowsTruncated &&
639+
excludedIncompleteEgressCount === 0 &&
640+
rollupWindowIsComplete &&
641+
throttleCoverageComplete;
590642
return {
591643
version: 2,
592644
generated_at: new Date(now).toISOString(),
593645
window: { hours: boundedHours, bucket_minutes: bucketKind === "five_minute" ? 5 : 60 },
594646
units: completeness.byUnit,
595647
rows: publicRows,
596648
rate_limit_observations: rateLimits,
649+
throttle_series: {
650+
unit: "wire_attempt",
651+
closed_through: new Date(closedThrough).toISOString(),
652+
first_available_bucket_start:
653+
firstAvailableBucket === null ? null : new Date(firstAvailableBucket).toISOString(),
654+
rows: throttleRows.slice(0, MAX_PUBLIC_THROTTLE_ROWS).map((row) => ({
655+
bucket_start: new Date(Number(row.bucket_start)).toISOString(),
656+
pool_class: row.pool_class,
657+
status_bucket: row.status_bucket,
658+
count: Number(row.count),
659+
})),
660+
rows_truncated: throttleRowsTruncated,
661+
excluded_incomplete_count: excludedIncompleteEgressCount,
662+
coverage_complete: throttleCoverageComplete,
663+
complete: throttleSeriesComplete,
664+
},
597665
retention: {
666+
rate_limit_detail_hours: RATE_LIMIT_RETENTION_MS / 3_600_000,
598667
rollup_evicted_rows_total: Number(
599668
diagnostics[
600669
bucketKind === "five_minute"

‎dashboard/public-observability.ts‎

Lines changed: 129 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const MAX_GITHUB_COUNT = 10_000_000_000;
1818
const MAX_DURATION_MS = 90 * 24 * 60 * 60 * 1000;
1919
const MAX_GITHUB_ROLLUP_ROWS = 2_000;
2020
const MAX_GITHUB_RATE_LIMIT_ROWS = 256;
21+
const MAX_GITHUB_THROTTLE_ROWS = 7 * 24 * GITHUB_EGRESS_POOL_CLASSES.length * 2;
2122

2223
const REVIEW_RANGES = ["6h", "24h", "7d"] as const;
2324
const REVIEW_LANES = ["exact_event", "hot_intake", "normal_backfill", "recovery"] as const;
@@ -860,6 +861,118 @@ function publicGithubRateLimits(value: unknown) {
860861
};
861862
}
862863

864+
function publicGithubThrottleSeries(
865+
value: unknown,
866+
hours: number,
867+
bucketMinutes: number,
868+
generatedAt: string,
869+
rollupWindowComplete: boolean,
870+
wireAttemptTotal: number,
871+
incompleteTotal: number,
872+
) {
873+
if (value === undefined) return undefined;
874+
const source = objectValue(value);
875+
const rawRows = Array.isArray(source?.rows) ? source.rows : null;
876+
const closedThrough = timestamp(source?.closed_through);
877+
const firstAvailableBucketStart = nullableTimestamp(source?.first_available_bucket_start);
878+
const excludedIncompleteCount = count(source?.excluded_incomplete_count, MAX_GITHUB_COUNT);
879+
if (
880+
!source ||
881+
source.unit !== "wire_attempt" ||
882+
!rawRows ||
883+
rawRows.length > MAX_GITHUB_THROTTLE_ROWS ||
884+
!closedThrough ||
885+
firstAvailableBucketStart === undefined ||
886+
excludedIncompleteCount === null ||
887+
excludedIncompleteCount > incompleteTotal ||
888+
typeof source.rows_truncated !== "boolean" ||
889+
typeof source.coverage_complete !== "boolean" ||
890+
typeof source.complete !== "boolean" ||
891+
(source.rows_truncated && rawRows.length !== MAX_GITHUB_THROTTLE_ROWS)
892+
) {
893+
return null;
894+
}
895+
const bucketMs = bucketMinutes * 60_000;
896+
const generatedMs = Date.parse(generatedAt);
897+
const closedMs = Date.parse(closedThrough);
898+
const firstAvailableMs =
899+
firstAvailableBucketStart === null ? null : Date.parse(firstAvailableBucketStart);
900+
const windowStart = Math.floor((generatedMs - hours * 3_600_000) / bucketMs) * bucketMs;
901+
if (
902+
closedMs !== Math.floor(generatedMs / bucketMs) * bucketMs ||
903+
(firstAvailableMs !== null &&
904+
(firstAvailableMs % bucketMs !== 0 || firstAvailableMs > closedMs))
905+
) {
906+
return null;
907+
}
908+
const rows = [];
909+
const seen = new Set<string>();
910+
let total = 0;
911+
let earliestRow = Number.POSITIVE_INFINITY;
912+
for (const entry of rawRows) {
913+
const row = objectValue(entry);
914+
const bucketStart = timestamp(row?.bucket_start);
915+
const rowCount = positiveCount(row?.count, MAX_GITHUB_COUNT);
916+
if (
917+
!row ||
918+
!bucketStart ||
919+
!member(GITHUB_EGRESS_POOL_CLASSES, row.pool_class) ||
920+
!member(["403", "429"], row.status_bucket) ||
921+
rowCount === null
922+
) {
923+
return null;
924+
}
925+
const bucketStartMs = Date.parse(bucketStart);
926+
const key = `${bucketStart}|${row.pool_class}|${row.status_bucket}`;
927+
total += rowCount;
928+
if (
929+
bucketStartMs % bucketMs !== 0 ||
930+
bucketStartMs < windowStart ||
931+
bucketStartMs >= closedMs ||
932+
seen.has(key) ||
933+
total > wireAttemptTotal
934+
) {
935+
return null;
936+
}
937+
seen.add(key);
938+
earliestRow = Math.min(earliestRow, bucketStartMs);
939+
rows.push({
940+
bucket_start: bucketStart,
941+
pool_class: row.pool_class,
942+
status_bucket: row.status_bucket,
943+
count: rowCount,
944+
});
945+
}
946+
if (
947+
(rows.length > 0 && firstAvailableMs === null) ||
948+
(firstAvailableMs !== null && firstAvailableMs > earliestRow)
949+
) {
950+
return null;
951+
}
952+
const coverageComplete = firstAvailableMs !== null && firstAvailableMs < windowStart;
953+
const complete =
954+
!source.rows_truncated &&
955+
excludedIncompleteCount === 0 &&
956+
rollupWindowComplete &&
957+
coverageComplete;
958+
if (source.coverage_complete !== coverageComplete || source.complete !== complete) return null;
959+
rows.sort((left, right) =>
960+
`${left.bucket_start}|${left.pool_class}|${left.status_bucket}`.localeCompare(
961+
`${right.bucket_start}|${right.pool_class}|${right.status_bucket}`,
962+
),
963+
);
964+
return {
965+
unit: "wire_attempt",
966+
closed_through: closedThrough,
967+
first_available_bucket_start: firstAvailableBucketStart,
968+
rows,
969+
rows_truncated: source.rows_truncated,
970+
excluded_incomplete_count: excludedIncompleteCount,
971+
coverage_complete: coverageComplete,
972+
complete,
973+
};
974+
}
975+
863976
export function publicGithubEgressObservabilityProjection(value: unknown) {
864977
const source = objectValue(value);
865978
const privacySource = objectValue(source?.privacy);
@@ -892,7 +1005,7 @@ export function publicGithubEgressObservabilityProjection(value: unknown) {
8921005
if (
8931006
source.version !== 2 ||
8941007
!generatedAt ||
895-
!member([0.25, 1, 6, 24], hours) ||
1008+
!member([0.25, 1, 6, 24, 168], hours) ||
8961009
window.bucket_minutes !== (Number(hours) <= 6 ? 5 : 60)
8971010
) {
8981011
return null;
@@ -963,13 +1076,25 @@ export function publicGithubEgressObservabilityProjection(value: unknown) {
9631076
(alreadyPublic
9641077
? rateLimits.total === MAX_GITHUB_RATE_LIMIT_ROWS
9651078
: rawRateLimits?.length === MAX_GITHUB_RATE_LIMIT_ROWS);
1079+
const throttleSeries = publicGithubThrottleSeries(
1080+
source.throttle_series,
1081+
Number(hours),
1082+
Number(window.bucket_minutes),
1083+
generatedAt,
1084+
completenessSource.rollup_window_complete,
1085+
units.wire_attempt,
1086+
incompleteCount,
1087+
);
1088+
const rateLimitDetailHours = retentionSource.rate_limit_detail_hours;
9661089
if (
9671090
completenessSource.observed !== expectedObserved ||
9681091
completenessSource.telemetry_complete !== expectedTelemetryComplete ||
9691092
completenessSource.query_complete !== expectedQueryComplete ||
9701093
unitTotal !== completenessTotal ||
9711094
!rowsConserved ||
972-
!rateLimitTruncationValid
1095+
!rateLimitTruncationValid ||
1096+
throttleSeries === null ||
1097+
(rateLimitDetailHours !== undefined && rateLimitDetailHours !== 24)
9731098
) {
9741099
return null;
9751100
}
@@ -980,7 +1105,9 @@ export function publicGithubEgressObservabilityProjection(value: unknown) {
9801105
units,
9811106
rows,
9821107
rate_limits: rateLimits,
1108+
...(throttleSeries === undefined ? {} : { throttle_series: throttleSeries }),
9831109
retention: {
1110+
...(rateLimitDetailHours === undefined ? {} : { rate_limit_detail_hours: 24 }),
9841111
rollup_evicted_rows_total: rollupEvictedRows,
9851112
rollup_eviction_count_exact: retentionSource.rollup_eviction_count_exact,
9861113
rate_limit_evicted_rows_total: rateLimitEvictedRows,

‎dashboard/worker.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4520,7 +4520,7 @@ async function publicReviewCoverageJson(env) {
45204520

45214521
async function publicGithubEgressObservabilityJson(env, params: URLSearchParams) {
45224522
const requested = Number(params.get("hours") || 6);
4523-
const hours = [0.25, 1, 6, 24].includes(requested) ? requested : 6;
4523+
const hours = [0.25, 1, 6, 24, 168].includes(requested) ? requested : 6;
45244524
return publicExactReviewProjectionJson(
45254525
env,
45264526
`/github-egress-observability?hours=${hours}`,

‎docs/github-egress-telemetry.md‎

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,34 @@ curl --fail --silent --show-error \
2626
'https://clawsweeper.openclaw.ai/api/github-egress-observability?hours=6'
2727
```
2828

29-
`hours` accepts only `0.25` (15 minutes), `1`, `6`, or `24`. Use the 15-minute
30-
view for periodic collection when a high-cardinality one-hour aggregate would
31-
reach the public row cap. The response contains revision-independent closed
32-
rollup dimensions and aggregate rate-limit counts. It never contains private
33-
pool identities, repository or item identifiers, deployment or configuration
34-
revision digests, branches, raw SHAs, paths, queries, cursors, URLs, request
35-
IDs, ETags, bodies, tokens, installation IDs, individual rate-limit rows, or raw
36-
header/reset values.
29+
`hours` accepts only `0.25` (15 minutes), `1`, `6`, `24`, or `168` (7 days).
30+
Use the 15-minute view for periodic collection when a high-cardinality one-hour
31+
aggregate would reach the public row cap. The response contains
32+
revision-independent closed rollup dimensions, a bounded `throttle_series`,
33+
and aggregate rate-limit counts. It never contains private pool identities,
34+
repository or item identifiers, deployment or configuration revision digests,
35+
branches, raw SHAs, paths, queries, cursors, URLs, request IDs, ETags, bodies,
36+
tokens, installation IDs, individual rate-limit rows, or raw header/reset
37+
values.
38+
39+
`throttle_series.rows` is the operational chart projection. It re-aggregates
40+
the existing rollup store by closed bucket, `pool_class`, and exact `403` or
41+
`429` status for complete, attempted `wire_attempt` rows whose outcome is
42+
`throttle`. It does not include invocations, pagination metadata, circuit
43+
deferrals, avoided requests, or raw rate-limit observations. The seven-day
44+
maximum is bounded at 1,344 rows: 168 hourly buckets, four closed pool classes,
45+
and two statuses. `closed_through` excludes the still-open clock bucket.
46+
`first_available_bucket_start`, `coverage_complete`, `rows_truncated`, and
47+
`complete` identify pre-instrumentation, retention, and cardinality boundaries
48+
without inventing zero-count history. Coverage is complete only when a retained
49+
bucket predates the requested lower-bound bucket; a first observation in that
50+
boundary bucket is conservatively treated as partial. Any incomplete egress
51+
evidence in the displayed closed window increments `excluded_incomplete_count`
52+
and makes `complete=false`, including an opaque invocation whose wire status
53+
could not be parsed. A parsed 403/429 whose attribution is incomplete is
54+
likewise omitted from the trustworthy pool split. Neither case can be rendered
55+
as a zero-throttle interval. Older cached version-2 responses without this
56+
projection remain readable through their detailed `rows` array.
3757

3858
The binding-only exact-review queue status retains a compact six-hour
3959
`publication.github_egress_metrics_v2` summary for internal workflows. The
@@ -236,11 +256,14 @@ method/status, and response receive time. Unsafe parsing emits or uploads an
236256
incomplete bounded marker. It never uploads a partially parsed raw frame.
237257

238258
Completeness is computed independently for each requested 15-minute, one-,
239-
six-, or 24-hour window. Rollup queries include the complete five-minute or
240-
hourly bucket that overlaps the window's lower boundary, so totals can include at
241-
most one bucket of observations immediately before the exact cutoff. Private
242-
rate-limit source selection uses the exact cutoff before aggregation.
243-
`rows_truncated` and
259+
six-, 24-hour, or seven-day window. Rollup queries include the complete
260+
five-minute or hourly bucket that overlaps the window's lower boundary, so
261+
totals can include at most one bucket of observations immediately before the
262+
exact cutoff. Private rate-limit source selection uses the exact cutoff before
263+
aggregation and is retained for only 24 hours; a seven-day response therefore
264+
always reports
265+
`rate_limit_window_complete=false` and `query_complete=false` even when its
266+
rollup-backed `throttle_series.complete` is true. `rows_truncated` and
244267
`rate_limit_rows_truncated` identify a bounded public response, while
245268
`rollup_window_complete` and `rate_limit_window_complete` compare the requested
246269
lower boundary with the latest timestamp actually removed by a cap. Running a

0 commit comments

Comments
 (0)