Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(connect): register --fresh-token and reuse the exchanged token for shared skills

garrytanbf7c34fbed9b
diff --git a/src/core/cli-flag-registry.generated.ts b/src/core/cli-flag-registry.generated.ts
index e23f26d2ea..5bb2e0d6b3 100644
--- a/src/core/cli-flag-registry.generated.ts
+++ b/src/core/cli-flag-registry.generated.ts
@@ -40,7 +40,7 @@ export const CLI_FLAG_REGISTRY: Record<string, readonly string[]> = {
'code-refs': ['--all-sources', '--brain', '--force', '--help', '--json', '--lang', '--limit', '--no-embed', '--no-json', '--source'],
'compile-context': ['--brain', '--budget', '--check', '--help', '--include', '--json', '--out', '--source', '--stdout', '--target'],
'config': ['--admin-intent', '--all', '--all-from', '--background', '--brain', '--call-site', '--coverage-override', '--dataset', '--db-url', '--egress', '--embedding-dimensions', '--embedding-model', '--expected-state', '--explain', '--follow', '--force', '--from', '--help', '--input', '--json', '--limit', '--markdown', '--min', '--out', '--path', '--pattern', '--pglite', '--progress-interval', '--progress-json', '--provider', '--query', '--quiet', '--raw', '--recommended', '--session', '--shadow', '--since', '--slot', '--source', '--status', '--suite', '--target', '--threshold', '--timeout', '--to', '--url', '--what-if-threshold', '--yes'],
- 'connect': ['--agent', '--auto', '--bearer-token-env-var', '--bind', '--brain', '--client-id', '--client-secret', '--credentials-file', '--env', '--force', '--grant-types', '--harness', '--header', '--help', '--http', '--install', '--issuer-url', '--json', '--mcp-only', '--mcp-url', '--name', '--oauth', '--oauth-client-id', '--oauth-client-secret', '--public-url', '--pure', '--register', '--remove', '--root', '--scope', '--scopes', '--show-token', '--source', '--status', '--timeout-ms', '--token', '--token-endpoint-auth-method', '--url', '--version', '--yes'],
+ 'connect': ['--agent', '--auto', '--bearer-token-env-var', '--bind', '--brain', '--client-id', '--client-secret', '--credentials-file', '--env', '--force', '--fresh-token', '--grant-types', '--harness', '--header', '--help', '--http', '--install', '--issuer-url', '--json', '--mcp-only', '--mcp-url', '--name', '--oauth', '--oauth-client-id', '--oauth-client-secret', '--public-url', '--pure', '--register', '--remove', '--root', '--scope', '--scopes', '--show-token', '--source', '--status', '--timeout-ms', '--token', '--token-endpoint-auth-method', '--url', '--version', '--yes'],
'connectors': ['--all', '--background', '--brain', '--cookie', '--dry-run', '--embed', '--explain', '--follow', '--force', '--full', '--help', '--install', '--json', '--limit', '--no-browser', '--progress-interval', '--progress-json', '--quiet', '--source', '--timeout', '--token', '--try-oauth', '--window-days'],
'conversation-parser': ['--brain', '--help', '--json', '--source'],
'creds': ['--allow-create-db', '--allow-docker', '--brain', '--chat-model', '--content-root', '--db-only', '--embedding-dimensions', '--embedding-model', '--entity', '--expansion-model', '--force', '--git', '--grant-types', '--help', '--http', '--ids', '--issuer-url', '--json', '--key', '--local-postgres', '--mcp-only', '--mcp-url', '--migrate-only', '--model', '--no-embedding', '--no-git', '--non-interactive', '--oauth-client-id', '--oauth-client-secret', '--out', '--passphrase-env', '--path', '--pglite', '--prefer-postgres', '--provenance', '--provider', '--schema-pack', '--scopes', '--skip-embed-check', '--source', '--status', '--supabase', '--surface', '--to', '--url', '--version'],
diff --git a/src/core/harness/install.ts b/src/core/harness/install.ts
index fd7c0edab4..56b3e89b52 100644
--- a/src/core/harness/install.ts
+++ b/src/core/harness/install.ts
@@ -79,6 +79,8 @@ function assertEntryOwned(entry: unknown, prior: Record<string, any>) {
/** Install only in the current environment. Foreign entries are never adopted. */
export async function installHarnessConnection(c: HarnessCredentials, opts: InstallOptions) {
+ // A cached unexpired token may be the invalidated one; every later step (config and shared skills) uses the new token.
+ if (opts.freshToken && !opts.remove) c = { ...c, access_token: await credentialAccessToken({ ...c, access_token: undefined }), expires_at: undefined };
const adapter = harnessAdapter(opts.harness);
const name = opts.name ?? 'gbrain';
if (!isValidName(name)) throw new Error('Invalid connection name');
@@ -116,7 +118,7 @@ export async function installHarnessConnection(c: HarnessCredentials, opts: Inst
const sibling = opencodeGlobalSiblingPath(configPath);
if (sibling && nativeEntry(sibling, adapter.connection, name) !== undefined) throw new Error('configuration_conflict: same-name entry in sibling opencode config; preserve it and choose one explicit configuration before installing');
}
- const token = opts.remove ? '' : await credentialAccessToken(opts.freshToken ? { ...c, access_token: undefined } : c);
+ const token = opts.remove ? '' : await credentialAccessToken(c);
const entry = adapter.connection === 'codex-toml' ? { url: c.mcp_url, http_headers: { Authorization: `Bearer ${token}` } }
: adapter.connection === 'opencode-json' ? { type: 'remote', url: c.mcp_url, headers: { Authorization: `Bearer ${token}` }, enabled: true }
: { type: 'http', url: c.mcp_url, headers: { Authorization: `Bearer ${token}` } };
diff --git a/test/harness-token-receipt.test.ts b/test/harness-token-receipt.test.ts
index 641008bb30..1df25e4641 100644
--- a/test/harness-token-receipt.test.ts
+++ b/test/harness-token-receipt.test.ts
@@ -95,4 +95,29 @@ describe('inline bearer token receipt (#5775)', () => {
expect(stdout + stderr).not.toContain(TOKEN);
expect(readFileSync(configPath, 'utf8')).toContain(TOKEN);
}, 60_000);
+
+ test('connect --install --fresh-token is accepted by the CLI and installs a newly exchanged token', async () => {
+ const dir = temp(), handoff = join(dir, 'handoff.json');
+ const issued = 'fixture-fresh-token-value-0002';
+ let exchanges = 0;
+ const server = Bun.serve({ port: 0, fetch: () => { exchanges++; return Response.json({ access_token: issued, token_type: 'Bearer', expires_in: 3600 }); } });
+ try {
+ const origin = `http://127.0.0.1:${server.port}`;
+ writeCredentials(handoff, { ...creds(), harness: 'codex', issuer_url: origin, mcp_url: `${origin}/mcp` });
+ const env: Record<string, string> = {};
+ for (const [key, value] of Object.entries(process.env)) if (value !== undefined) env[key] = value;
+ for (const key of ['DATABASE_URL', 'GBRAIN_DATABASE_URL']) delete env[key];
+ Object.assign(env, { HOME: dir, GBRAIN_HOME: dir, CODEX_HOME: join(dir, '.codex'), GBRAIN_SELF_UPGRADE_MODE: 'off', GBRAIN_SKIP_STARTUP_HOOKS: '1' });
+ const proc = Bun.spawn(['bun', 'run', join(REPO_ROOT, 'src', 'cli.ts'), 'connect', `${origin}/mcp`, '--harness', 'codex', '--credentials-file', handoff, '--install', '--fresh-token', '--json'],
+ { cwd: dir, env, stdout: 'pipe', stderr: 'pipe' });
+ const [stdout, stderr, code] = await Promise.all([new Response(proc.stdout).text(), new Response(proc.stderr).text(), proc.exited]);
+ expect({ code, stderr }).toMatchObject({ code: 0 });
+ expect(JSON.parse(stdout)).toMatchObject({ status: 'installed', token_storage: 'inline' });
+ expect(exchanges).toBe(1);
+ const config = readFileSync(join(dir, '.codex', 'config.toml'), 'utf8');
+ expect(config).toContain(issued);
+ expect(config).not.toContain(TOKEN);
+ expect(stdout + stderr).not.toContain(issued);
+ } finally { server.stop(true); }
+ }, 60_000);
});