Panniantong/Agent-Reach · error · GitHubConfigError

gh hosts.yml 的 github.com 配置无效

Error message

gh hosts.yml 的 github.com 配置无效

What it means

Raised when the top-level 'github.com' key exists in hosts.yml but its value is not a mapping (scalar, list, or None-shape mismatch). The per-host entry must be a dict holding keys like oauth_token, user, users.

Source

Thrown at agent_reach/channels/github.py:76

        )
    except (OSError, PrivatePathError, UnicodeError) as exc:
        raise GitHubConfigError("gh hosts.yml 无法安全读取") from exc
    if raw is None:
        return False
    try:
        payload = yaml.safe_load(raw)
    except yaml.YAMLError as exc:
        raise GitHubConfigError("gh hosts.yml 不是有效的 UTF-8 YAML") from exc
    if payload is None:
        return False
    if not isinstance(payload, dict):
        raise GitHubConfigError("gh hosts.yml 顶层必须是对象")

    host = payload.get("github.com")
    if host is None:
        return False
    if not isinstance(host, dict):
        raise GitHubConfigError("gh hosts.yml 的 github.com 配置无效")

    users = host.get("users")
    if users is not None and not isinstance(users, dict):
        raise GitHubConfigError("gh hosts.yml 的 users 配置无效")
    return bool(host.get("oauth_token") or host.get("user") or users)


def _explicit_github_credentials(config) -> bool:
    if any(os.environ.get(name) for name in ("GH_TOKEN", "GITHUB_TOKEN")):
        return True
    if config is None:
        return False
    try:
        return bool(config.get("github_token"))
    except Exception as exc:
        raise GitHubConfigError("Agent Reach 的 GitHub 配置无法读取") from exc

View on GitHub (pinned to 93ae1d18c3)

Solutions

  1. Fix the github.com entry to a nested mapping: 'github.com:\n oauth_token: ...\n user: ...'
  2. Run `gh auth status` — if it also complains, re-login with `gh auth login`
  3. Validate locally: python -c "import yaml; d=yaml.safe_load(open('$HOME/.config/gh/hosts.yml')); assert isinstance(d['github.com'], dict)"

Example fix

# before
 github.com: gho_abc123
# after
 github.com:
   oauth_token: gho_abc123
   user: myname
Defensive patterns

Strategy: validation

Validate before calling

import yaml

def github_entry_valid(path) -> bool:
    try:
        d = yaml.safe_load(open(path, encoding="utf-8").read()) or {}
    except Exception:
        return False
    host = d.get("github.com")
    return host is None or isinstance(host, dict)

Try / catch

except GitHubConfigError as exc:
    if "github.com 配置无效" in str(exc):
        hint = "rewrite the github.com block as a nested mapping or re-run gh auth login"

Prevention

When it happens

Trigger: hosts.yml has 'github.com: some string' or 'github.com: [a, b]'. payload.get('github.com') returns a non-dict, isinstance check fails, error raised. A missing 'github.com' key returns False quietly (no error).

Common situations: Hand-editing that flattens the structure; YAML anchor reuse producing a list of hosts; template systems writing 'github.com: {{ token }}' with the placeholder unexpanded.

Related errors


AI-assisted analysis of Panniantong/Agent-Reach@93ae1d18c3 (2026-08-14). Data as JSON: /api/errors/6f85bd6bb766d787. Report an issue: GitHub.