Skip to content

Open-source SEO, GEO, and marketing skills for AI agents.

fix(openclaw): retry on "config changed since last load" + redact bearers

Yuting Zhong4055e6097fed
diff --git a/src/app/api/mcp-oauth/callback/route.ts b/src/app/api/mcp-oauth/callback/route.ts
index 7a4bbe02..ab4d994b 100644
--- a/src/app/api/mcp-oauth/callback/route.ts
+++ b/src/app/api/mcp-oauth/callback/route.ts
@@ -98,10 +98,15 @@ export async function GET(request: Request) {
try {
await setMcpBearer(pending.stored_key, pending.resource_url, access_token);
} catch (err) {
- back.searchParams.set(
- "mcp_error",
- `Saving MCP config failed: ${err instanceof Error ? err.message : String(err)}`,
- );
+ const raw = err instanceof Error ? err.message : String(err);
+ // Scrub bearers + any oat_ token shape before exposing in the URL —
+ // the redirect lands in browser history, server logs, and Referer
+ // headers on the destination page. Leaking the access token there
+ // would let any local-machine reader replay calls against the MCP.
+ const scrubbed = raw
+ .replace(/Bearer\s+[A-Za-z0-9._-]+/gi, "Bearer [redacted]")
+ .replace(/oat_[A-Za-z0-9_]+/gi, "oat_[redacted]");
+ back.searchParams.set("mcp_error", `Saving MCP config failed: ${scrubbed}`);
return NextResponse.redirect(back);
}
diff --git a/src/server/openclaw/cli.ts b/src/server/openclaw/cli.ts
index 4c2bbe1b..f7613327 100644
--- a/src/server/openclaw/cli.ts
+++ b/src/server/openclaw/cli.ts
@@ -47,7 +47,7 @@ export async function openclaw(
args: string[],
options: OpenClawOptions = {},
): Promise<unknown> {
- const run = () => runOpenclaw(args, options);
+ const run = () => runOpenclawWithRetry(args, options);
const next = openclawQueue.then(run, run);
// Keep the chain alive even when a call rejects — without `.catch`
// a single failure would propagate forever to subsequent callers.
@@ -55,6 +55,44 @@ export async function openclaw(
return next;
}
+/**
+ * The OpenClaw CLI does optimistic concurrency on ~/.openclaw/openclaw.json:
+ * it loads at startup, and on write checks whether the file changed
+ * underneath it. When the gateway daemon (a separate process) writes
+ * between our load and our write, we get exit code 1 with stderr:
+ *
+ * [openclaw] Could not start the CLI.
+ * [openclaw] Reason: config changed since last load
+ *
+ * This is a transient cross-process race that our in-process mutex
+ * can't prevent. Retry a couple of times — each attempt re-reads the
+ * config, so a winning retry is essentially "no overlap this time".
+ */
+async function runOpenclawWithRetry(
+ args: string[],
+ options: OpenClawOptions,
+): Promise<unknown> {
+ const maxAttempts = 3;
+ let lastErr: unknown;
+ for (let attempt = 1; attempt <= maxAttempts; attempt++) {
+ try {
+ return await runOpenclaw(args, options);
+ } catch (err) {
+ lastErr = err;
+ if (!(err instanceof OpenClawError)) throw err;
+ const transient =
+ err.exitCode === 1 &&
+ /config changed since last load|EAGAIN|EBUSY/i.test(err.stderr);
+ if (!transient || attempt === maxAttempts) throw err;
+ // Small backoff with jitter so two competing callers don't lock-
+ // step into the same retry slot forever.
+ const delayMs = 150 * attempt + Math.floor(Math.random() * 100);
+ await new Promise((r) => setTimeout(r, delayMs));
+ }
+ }
+ throw lastErr;
+}
+
function runOpenclaw(args: string[], options: OpenClawOptions): Promise<unknown> {
const timeout = options.timeout ?? 30_000;
const wantJson = options.json ?? true;