Skip to content

Commit ae6a061

Browse files
recuu-pfegclaude
andcommitted
fix: cache git credential helper token for 50 minutes
Previously fetched a fresh GitHub App token on every git operation, causing excessive API calls (every 3 seconds during agent work). Now caches to ~/.toban/.git-token-cache with 50-minute TTL. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent a6e9ded commit ae6a061

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

src/git-ops.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,29 @@ import * as ui from "./ui.js";
1414
*/
1515
export function setupGitCredentialHelper(tobanHome: string, apiUrl: string, apiKey: string): string {
1616
const helperPath = join(tobanHome, "git-credential-helper.sh");
17+
const cachePath = join(tobanHome, ".git-token-cache");
1718
const script = `#!/bin/sh
1819
# Toban git credential helper — fetches fresh GitHub App token on demand
20+
# Caches token for 50 minutes (GitHub App tokens expire in 1 hour)
1921
if [ "$1" = "get" ]; then
22+
CACHE_FILE="${cachePath}"
23+
CACHE_MAX_AGE=3000
24+
if [ -f "$CACHE_FILE" ]; then
25+
CACHE_AGE=$(( $(date +%s) - $(stat -f %m "$CACHE_FILE" 2>/dev/null || stat -c %Y "$CACHE_FILE" 2>/dev/null || echo 0) ))
26+
if [ "$CACHE_AGE" -lt "$CACHE_MAX_AGE" ]; then
27+
cat "$CACHE_FILE"
28+
exit 0
29+
fi
30+
fi
2031
TOKEN=$(curl -sf -H "Authorization: Bearer ${apiKey}" "${apiUrl}/api/v1/workspace/git-token" | sed -n 's/.*"token":"\\([^"]*\\)".*/\\1/p')
2132
if [ -n "$TOKEN" ]; then
22-
echo "protocol=https"
23-
echo "host=github.com"
24-
echo "username=x-access-token"
25-
echo "password=$TOKEN"
33+
OUTPUT="protocol=https
34+
host=github.com
35+
username=x-access-token
36+
password=$TOKEN"
37+
echo "$OUTPUT" > "$CACHE_FILE"
38+
chmod 600 "$CACHE_FILE"
39+
echo "$OUTPUT"
2640
fi
2741
fi
2842
`;

0 commit comments

Comments
 (0)