Skip to content

Commit d3810a0

Browse files
authored
perf(github): route sweep public reads off App tokens (#1062)
* perf(github): route sweep public reads off App tokens Extend the existing public-read token convention to sweep review, publication, comment-sync, audit, and apply paths while preserving App credentials for mutations and proof inspection. Public-token throttles consume at most one ambient App-token fallback per process. * fix(github): route header reads through public token * fix(github): route scheduled review reads
1 parent d7f1836 commit d3810a0

7 files changed

Lines changed: 329 additions & 33 deletions

‎.github/workflows/sweep.yml‎

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,8 +1077,8 @@ jobs:
10771077
if: ${{ steps.claim-exact-review-queue.outputs.claimed == 'true' && steps.live-item.outputs.proceed == 'true' && steps.reserve-exact-review-lease.outputs.status == 'posted' }}
10781078
continue-on-error: true
10791079
env:
1080-
GH_TOKEN: ${{ steps.target-read-token.outputs.token }}
1081-
CLAWSWEEPER_PROOF_INSPECTION_TOKEN: ${{ steps.target-read-token.outputs.token || github.token }}
1080+
GH_TOKEN: ${{ steps.target.outputs.target_repo == 'openclaw/openclaw' && github.token || steps.target-read-token.outputs.token }}
1081+
CLAWSWEEPER_PROOF_INSPECTION_TOKEN: ${{ steps.target-read-token.outputs.token }}
10821082
ADDITIONAL_PROMPT: ${{ fromJSON(steps.claim-exact-review-queue.outputs.decision).additionalPrompt || '' }}
10831083
CLAWSWEEPER_RELATED_GITHUB_SEARCH: ${{ vars.CLAWSWEEPER_RELATED_GITHUB_SEARCH || '1' }}
10841084
EXACT_REVIEW_ITEM_KEY: ${{ steps.claim-exact-review-queue.outputs.item_key }}
@@ -1349,6 +1349,7 @@ jobs:
13491349
env:
13501350
CLAWSWEEPER_WEBHOOK_SECRET: ${{ secrets.CLAWSWEEPER_WEBHOOK_SECRET }}
13511351
GH_TOKEN: ${{ steps.target-write-token.outputs.token }}
1352+
CLAWSWEEPER_PUBLIC_GH_TOKEN: ${{ steps.target.outputs.target_repo == 'openclaw/openclaw' && github.token || '' }}
13521353
REPO_TOKEN: ${{ github.token }}
13531354
TARGET_REPO: ${{ steps.target.outputs.target_repo }}
13541355
ITEM_NUMBER: ${{ steps.target.outputs.item_number }}
@@ -2333,6 +2334,7 @@ jobs:
23332334
env:
23342335
CLAWSWEEPER_WEBHOOK_SECRET: ${{ secrets.CLAWSWEEPER_WEBHOOK_SECRET }}
23352336
GH_TOKEN: ${{ steps.target-write-token.outputs.token }}
2337+
CLAWSWEEPER_PUBLIC_GH_TOKEN: ${{ steps.publication-context.outputs.target_repo == 'openclaw/openclaw' && github.token || '' }}
23362338
REPO_TOKEN: ${{ github.token }}
23372339
TARGET_REPO: ${{ steps.publication-context.outputs.target_repo }}
23382340
ITEM_NUMBER: ${{ steps.publication-context.outputs.item_number }}
@@ -2697,7 +2699,7 @@ jobs:
26972699
id: confirm-terminal-item
26982700
if: ${{ always() && steps.publication-context.outputs.claimed == 'true' && steps.publication-context.outputs.direct_lifecycle_recovery != 'true' && steps.release-terminal-review-leases.outcome == 'success' }}
26992701
env:
2700-
GH_TOKEN: ${{ steps.target-write-token.outputs.token }}
2702+
GH_TOKEN: ${{ steps.publication-context.outputs.target_repo == 'openclaw/openclaw' && github.token || steps.target-write-token.outputs.token }}
27012703
TARGET_REPO: ${{ steps.publication-context.outputs.target_repo }}
27022704
ITEM_NUMBER: ${{ steps.publication-context.outputs.item_number }}
27032705
run: |
@@ -4166,7 +4168,8 @@ jobs:
41664168
working-directory: clawsweeper
41674169
env:
41684170
GH_TOKEN: ${{ steps.target-read-token.outputs.token || github.token }}
4169-
CLAWSWEEPER_PROOF_INSPECTION_TOKEN: ${{ steps.codex-inspection-token.outputs.token || github.token }}
4171+
CLAWSWEEPER_PUBLIC_GH_TOKEN: ${{ needs.plan.outputs.target_repo == 'openclaw/openclaw' && github.token || '' }}
4172+
CLAWSWEEPER_PROOF_INSPECTION_TOKEN: ${{ steps.codex-inspection-token.outputs.token }}
41704173
ADDITIONAL_PROMPT: ${{ github.event.inputs.additional_prompt || github.event.client_payload.review_options.additional_prompt || github.event.client_payload.additional_prompt || '' }}
41714174
EXACT_ITEM: ${{ github.event.client_payload.item_number || github.event.inputs.item_number || github.event.inputs.item_numbers || '' }}
41724175
EXPECTED_SOURCE_REVISION: ${{ github.event.client_payload.expected_source_revision || '' }}
@@ -4768,6 +4771,7 @@ jobs:
47684771
env:
47694772
CLAWSWEEPER_WEBHOOK_SECRET: ${{ secrets.CLAWSWEEPER_WEBHOOK_SECRET }}
47704773
GH_TOKEN: ${{ steps.target-write-token.outputs.token }}
4774+
CLAWSWEEPER_PUBLIC_GH_TOKEN: ${{ needs.plan.outputs.target_repo == 'openclaw/openclaw' && github.token || '' }}
47714775
QUEUE_URL: ${{ vars.CLAWSWEEPER_EXACT_REVIEW_QUEUE_URL || 'https://clawsweeper.openclaw.ai' }}
47724776
TARGET_REPO: ${{ needs.plan.outputs.target_repo }}
47734777
run: |
@@ -5276,15 +5280,15 @@ jobs:
52765280
- name: Select target read token
52775281
id: target-token
52785282
env:
5279-
APP_TOKEN: ${{ steps.target-read-token.outputs.token }}
5280-
FALLBACK_TOKEN: ${{ github.token }}
5283+
PRIMARY_TOKEN: ${{ github.token }}
5284+
APP_FALLBACK_TOKEN: ${{ steps.target-read-token.outputs.token }}
52815285
run: |
5282-
if [ -n "$APP_TOKEN" ]; then
5283-
echo "Using ClawSweeper App token for audit reads."
5284-
echo "token=$APP_TOKEN" >> "$GITHUB_OUTPUT"
5286+
if [ -n "$PRIMARY_TOKEN" ]; then
5287+
echo "Using workflow token for public audit reads."
5288+
echo "token=$PRIMARY_TOKEN" >> "$GITHUB_OUTPUT"
52855289
else
5286-
echo "Using workflow token fallback for public audit reads."
5287-
echo "token=$FALLBACK_TOKEN" >> "$GITHUB_OUTPUT"
5290+
echo "Using ClawSweeper App token fallback for audit reads."
5291+
echo "token=$APP_FALLBACK_TOKEN" >> "$GITHUB_OUTPUT"
52885292
fi
52895293
52905294
- name: Create state token
@@ -5416,8 +5420,12 @@ jobs:
54165420
exit 1
54175421
fi
54185422
target_slug="$(printf '%s' "$target_repo" | tr '[:upper:]' '[:lower:]' | sed -E 's/[^a-z0-9_.-]+/-/g')"
5423+
target_owner="${target_repo%%/*}"
5424+
target_name="${target_repo#*/}"
54195425
echo "target_repo=$target_repo" >> "$GITHUB_OUTPUT"
54205426
echo "target_slug=$target_slug" >> "$GITHUB_OUTPUT"
5427+
echo "target_repo_owner=$target_owner" >> "$GITHUB_OUTPUT"
5428+
echo "target_repo_name=$target_name" >> "$GITHUB_OUTPUT"
54215429
54225430
- name: Resolve proof artifact identity
54235431
id: proof-artifact
@@ -5531,14 +5539,28 @@ jobs:
55315539
- uses: ./.github/actions/setup-openclaw
55325540
if: ${{ steps.proof-select.outputs.item_numbers != '' }}
55335541

5542+
- name: Create target proof inspection token
5543+
id: proof-inspection-token
5544+
if: ${{ steps.proof-select.outputs.item_numbers != '' }}
5545+
continue-on-error: true
5546+
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
5547+
with:
5548+
client-id: ${{ env.CLAWSWEEPER_APP_CLIENT_ID }}
5549+
private-key: ${{ secrets.CLAWSWEEPER_APP_PRIVATE_KEY }}
5550+
owner: ${{ steps.target.outputs.target_repo_owner }}
5551+
repositories: ${{ steps.target.outputs.target_repo_name }}
5552+
permission-contents: read
5553+
permission-issues: read
5554+
permission-pull-requests: read
5555+
55345556
- name: Generate bound close coverage proofs
55355557
id: generate-apply-proofs
55365558
if: ${{ steps.proof-select.outputs.item_numbers != '' }}
55375559
timeout-minutes: 50
55385560
env:
55395561
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
55405562
GH_TOKEN: ${{ github.token }}
5541-
CLAWSWEEPER_PROOF_INSPECTION_TOKEN: ${{ github.token }}
5563+
CLAWSWEEPER_PROOF_INSPECTION_TOKEN: ${{ steps.proof-inspection-token.outputs.token }}
55425564
TARGET_REPO: ${{ steps.target.outputs.target_repo }}
55435565
run: |
55445566
set -euo pipefail
@@ -5806,7 +5828,7 @@ jobs:
58065828
if: ${{ !(github.event_name == 'schedule' && github.event.schedule == '6,21,36,51 * * * *') && !(github.event_name == 'workflow_dispatch' && (github.event.inputs.apply_sync_comments_only == 'true' || github.event.inputs.apply_item_numbers == '__cursor__')) }}
58075829
env:
58085830
CLAWSWEEPER_WEBHOOK_SECRET: ${{ secrets.CLAWSWEEPER_WEBHOOK_SECRET }}
5809-
GH_TOKEN: ${{ steps.target-write-token.outputs.token }}
5831+
GH_TOKEN: ${{ steps.target.outputs.target_repo == 'openclaw/openclaw' && github.token || steps.target-write-token.outputs.token }}
58105832
QUEUE_URL: ${{ vars.CLAWSWEEPER_EXACT_REVIEW_QUEUE_URL || 'https://clawsweeper.openclaw.ai' }}
58115833
TARGET_REPO: ${{ steps.target.outputs.target_repo }}
58125834
run: |
@@ -5822,6 +5844,7 @@ jobs:
58225844
env:
58235845
CLAWSWEEPER_WEBHOOK_SECRET: ${{ secrets.CLAWSWEEPER_WEBHOOK_SECRET }}
58245846
GH_TOKEN: ${{ steps.target-write-token.outputs.token }}
5847+
CLAWSWEEPER_PUBLIC_GH_TOKEN: ${{ steps.target.outputs.target_repo == 'openclaw/openclaw' && github.token || '' }}
58255848
CLAWSWEEPER_APPLY_TOKEN_MINTED_AT_MS: ${{ steps.target-write-token.outputs.minted-at-ms }}
58265849
QUEUE_URL: ${{ vars.CLAWSWEEPER_EXACT_REVIEW_QUEUE_URL || 'https://clawsweeper.openclaw.ai' }}
58275850
TARGET_REPO: ${{ steps.target.outputs.target_repo }}

‎src/clawsweeper-github-execution.ts‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export function createGitHubExecution(dependencies: CreateGitHubExecutionDepende
2424
const { ROOT, gitHubRuntime, labelAlreadyExistsError } = dependencies;
2525
const {
2626
GitHubRuntimeBudgetError,
27+
claimPublicReadFallback,
2728
ensureGitHubRetryFits,
2829
ensureGitHubRuntimeAvailable,
2930
gh,
@@ -38,14 +39,45 @@ export function createGitHubExecution(dependencies: CreateGitHubExecutionDepende
3839
attempts = configuredGitHubRetryAttempts(),
3940
options: GitHubRetryOptions = {},
4041
): string {
42+
let activeEnv: NodeJS.ProcessEnv | undefined;
4143
let lastError: unknown;
4244
for (let attempt = 0; attempt < attempts; attempt += 1) {
4345
try {
44-
return options.request?.(args, attempt) ?? gh(args);
46+
return (
47+
options.request?.(args, attempt) ??
48+
(activeEnv ? ghWithPreparedTimeout(args, githubCommandTimeoutMs(), activeEnv) : gh(args))
49+
);
4550
} catch (error) {
4651
if (error instanceof GitHubRuntimeBudgetError) throw error;
4752
lastError = error;
4853
const retryKind = ghRetryKind(error);
54+
const fallback =
55+
retryKind === "throttle" && !options.request ? claimPublicReadFallback(args) : null;
56+
if (retryKind === "throttle" && fallback) {
57+
activeEnv = fallback;
58+
try {
59+
return ghWithPreparedTimeout(args, githubCommandTimeoutMs(), fallback);
60+
} catch (fallbackError) {
61+
if (fallbackError instanceof GitHubRuntimeBudgetError) throw fallbackError;
62+
lastError = fallbackError;
63+
const fallbackRetryKind = ghRetryKind(fallbackError);
64+
if (fallbackRetryKind === "throttle") {
65+
throw new GitHubRateLimitError(fallbackError);
66+
}
67+
ensureGitHubRuntimeAvailable("after GitHub operation");
68+
if (fallbackRetryKind === "none" || attempt === attempts - 1) {
69+
throw fallbackError;
70+
}
71+
const waitMs = ghRetryWaitMs(fallbackRetryKind, attempt);
72+
ensureGitHubRetryFits(waitMs);
73+
console.error(
74+
`Transient GitHub API failure; retrying ${summarizeGhArgs(args)} in ${Math.round(waitMs / 1000)}s`,
75+
);
76+
if (options.sleepBeforeRetry) options.sleepBeforeRetry(waitMs);
77+
else sleepBeforeGitHubRetry(waitMs);
78+
continue;
79+
}
80+
}
4981
if (retryKind === "throttle") throw new GitHubRateLimitError(error);
5082
ensureGitHubRuntimeAvailable("after GitHub operation");
5183
if (retryKind === "none" || attempt === attempts - 1) throw error;

‎src/clawsweeper-github-runtime.ts‎

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import { spawnSync } from "node:child_process";
22
import type { GitHubRuntimeBudget } from "./clawsweeper-types.js";
33
import { codexEnv } from "./codex-env.js";
44
import { resolveCommand } from "./command.js";
5-
import { exactPublicationPublicReadToken } from "./github-public-read.js";
5+
import {
6+
exactPublicationPublicReadToken,
7+
isPublicOpenClawReadOnlyRequest,
8+
} from "./github-public-read.js";
69

710
interface CreateGitHubRuntimeDependencies {
811
ROOT: string;
@@ -14,6 +17,8 @@ interface CreateGitHubRuntimeDependencies {
1417
targetRepo: () => string;
1518
}
1619

20+
const claimedPublicReadFallbackTokens = new Set<string>();
21+
1722
export function createGitHubRuntime(dependencies: CreateGitHubRuntimeDependencies) {
1823
const { ROOT, run, targetRepo } = dependencies;
1924

@@ -98,12 +103,67 @@ export function createGitHubRuntime(dependencies: CreateGitHubRuntimeDependencie
98103
sleepMs(waitMs);
99104
}
100105

101-
function ghWithPreparedTimeout(args: string[], timeoutMs: number | undefined): string {
106+
function publicReadToken(
107+
args: readonly string[],
108+
overrides: NodeJS.ProcessEnv = {},
109+
): string | null {
110+
const publicToken = process.env.CLAWSWEEPER_PUBLIC_GH_TOKEN?.trim();
111+
const env = { ...process.env, ...overrides };
112+
if (
113+
!publicToken ||
114+
Object.hasOwn(overrides, "GH_TOKEN") ||
115+
Object.hasOwn(overrides, "GITHUB_TOKEN") ||
116+
(env.GH_HOST && env.GH_HOST.toLowerCase() !== "github.com") ||
117+
!isPublicOpenClawReadOnlyRequest(args)
118+
) {
119+
return null;
120+
}
121+
return publicToken;
122+
}
123+
124+
function preparedGitHubEnv(
125+
args: readonly string[],
126+
overrides: NodeJS.ProcessEnv = {},
127+
): NodeJS.ProcessEnv | undefined {
128+
const hasExplicitToken =
129+
Object.hasOwn(overrides, "GH_TOKEN") || Object.hasOwn(overrides, "GITHUB_TOKEN");
130+
const token =
131+
publicReadToken(args, overrides) ??
132+
(hasExplicitToken
133+
? null
134+
: exactPublicationPublicReadToken(args, targetRepo(), {
135+
...process.env,
136+
...overrides,
137+
}));
138+
if (token) return { ...overrides, GH_TOKEN: token };
139+
return Object.keys(overrides).length > 0 ? overrides : undefined;
140+
}
141+
142+
function claimPublicReadFallback(args: readonly string[]): NodeJS.ProcessEnv | null {
143+
const publicToken = publicReadToken(args);
144+
const appToken = process.env.GH_TOKEN?.trim();
145+
if (
146+
!publicToken ||
147+
!appToken ||
148+
publicToken === appToken ||
149+
claimedPublicReadFallbackTokens.has(appToken)
150+
) {
151+
return null;
152+
}
153+
claimedPublicReadFallbackTokens.add(appToken);
154+
return { GH_TOKEN: appToken };
155+
}
156+
157+
function ghWithPreparedTimeout(
158+
args: string[],
159+
timeoutMs: number | undefined,
160+
env: NodeJS.ProcessEnv = {},
161+
): string {
102162
const resolvedArgs = args[0] === "api" ? args : ["--repo", targetRepo(), ...args];
103-
const publicReadToken = exactPublicationPublicReadToken(resolvedArgs, targetRepo());
163+
const preparedEnv = preparedGitHubEnv(resolvedArgs, env);
104164
return run("gh", resolvedArgs, {
105165
timeoutMs,
106-
...(publicReadToken ? { env: { GH_TOKEN: publicReadToken } } : {}),
166+
...(preparedEnv ? { env: preparedEnv } : {}),
107167
});
108168
}
109169

@@ -113,11 +173,10 @@ export function createGitHubRuntime(dependencies: CreateGitHubRuntimeDependencie
113173

114174
function ghOnce(args: string[], timeoutMs: number): string {
115175
const resolvedArgs = args[0] === "api" ? args : ["--repo", targetRepo(), ...args];
116-
const publicReadToken = exactPublicationPublicReadToken(resolvedArgs, targetRepo());
117176
const env = {
118177
...process.env,
119178
GIT_OPTIONAL_LOCKS: "0",
120-
...(publicReadToken ? { GH_TOKEN: publicReadToken } : {}),
179+
...preparedGitHubEnv(resolvedArgs),
121180
};
122181
const command = resolveCommand("gh", resolvedArgs, env);
123182
const commandTimeoutMs = githubCommandTimeoutMs(timeoutMs) ?? timeoutMs;
@@ -181,6 +240,7 @@ export function createGitHubRuntime(dependencies: CreateGitHubRuntimeDependencie
181240

182241
return {
183242
GitHubRuntimeBudgetError,
243+
claimPublicReadFallback,
184244
ensureGitHubRetryFits,
185245
ensureGitHubRuntimeAvailable,
186246
ensureRuntimeDelayFits,

‎src/github-public-read.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export function isPublicOpenClawReadOnlyRequest(ghArgs: readonly string[]): boolean {
22
if (ghArgs[0] !== "api") return false;
3-
const endpoint = ghArgs[1] ?? "";
3+
const endpointIndex = ghArgs[1] === "-i" ? 2 : 1;
4+
const endpoint = ghArgs[endpointIndex] ?? "";
45
const endpointPath = endpoint.split("?", 1)[0] ?? "";
56
if (!/^repos\/openclaw\/openclaw\/(?:issues|pulls)(?:\/|$)/.test(endpointPath)) {
67
return false;
@@ -13,7 +14,7 @@ export function isPublicOpenClawReadOnlyRequest(ghArgs: readonly string[]): bool
1314
return false;
1415
}
1516

16-
for (let index = 2; index < ghArgs.length; index += 1) {
17+
for (let index = endpointIndex + 1; index < ghArgs.length; index += 1) {
1718
const flag = ghArgs[index];
1819
if (flag === "--paginate" || flag === "--slurp") continue;
1920
if ((flag === "--jq" || flag === "-q") && index + 1 < ghArgs.length) {

‎test/clawsweeper.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2205,7 +2205,10 @@ test("sweep workflow executes only durable queue leases without runner-side admi
22052205
assert.match(legacyIntakeBlock, /statusCommentId: payload\.status_comment_id/);
22062206
assert.match(legacyIntakeBlock, /additionalPrompt: payload\.additional_prompt/);
22072207
assert.match(eventReviewBlock, /cancel-in-progress: false/);
2208-
assert.match(exactReviewStep, /GH_TOKEN: \$\{\{ steps\.target-read-token\.outputs\.token \}\}/);
2208+
assert.match(
2209+
exactReviewStep,
2210+
/GH_TOKEN: \$\{\{ steps\.target\.outputs\.target_repo == 'openclaw\/openclaw' && github\.token \|\| steps\.target-read-token\.outputs\.token \}\}/,
2211+
);
22092212
assert.match(exactReviewStep, /--readonly-openclaw/);
22102213
assert.match(exactReviewStep, /--skip-start-comment/);
22112214
assert.ok(claimIndex >= 0);

0 commit comments

Comments
 (0)