feat(tools): forge.py login --token-stdin, a personal access token for a second machine #473

Closed
mooki wants to merge 3 commits from mooki/Longshot:feat/forge-token-login into main
Member

Type: feat · Risk: low · Tested on: macOS 26.7.1, Apple M4 Max (game-free checks against a stand-in Forgejo, no game run) · Needs: Windows and Linux, no GPU needed (a real token on git.magicbean.dev)

Why

Signing in with python -I scripts/forge.py login on a second machine signs you out on the first within about an
hour, and signing in again there does the same to the second. Someone who works on two machines can't stay signed in
on both. Fixes #435.

Forgejo keeps one OAuth grant per user per application, and every machine signs in through the same app
(GIT_CLIENT_ID in scripts/magicbean_login.py). In Forgejo's routers/web/auth/oauth.go (v17.0.1, the version
git.magicbean.dev reports):

  • a sign-in reuses the user's grant for the app (app.GetGrantByUserID, line 617);
  • every token issued, at a sign-in or a refresh, increments the grant's counter when InvalidateRefreshTokens is on
    (lines 161-162);
  • a refresh token carrying an older count is refused with unauthorized_client, "token was already used" (lines
    827-831).

git.magicbean.dev has InvalidateRefreshTokens on. So when machine A signs in or renews, machine B's refresh token goes
stale; B works until its access token runs out (about an hour), then its renewal fails:

the git.magicbean.dev sign-in could not be renewed (POST https://git.magicbean.dev/login/oauth/access_token: HTTP 400
{"error":"unauthorized_client","error_description":"token was already used"}): sign in again

The _SessionLock fixed this race between two processes on one machine; it can't help across machines.

What changed

forge.py login --token-stdin signs in with a Forgejo personal access token instead of Discord. The token has no
refresh token and is never renewed, so it leaves the one OAuth grant alone and no machine signs another out; when
Forgejo refuses it, forge.py says to make a new one. Plain login, the Discord sign-in and its renewal are unchanged.

  • Read first: token_sign_in, forge_session and forge_expire in magicbean_login.py, then cmd_login and api in
    forge.py.
  • tools: python -I scripts/forge.py login --token-stdin (cmd_login and read_token in scripts/forge.py,
    token_sign_in in scripts/magicbean_login.py).
    • It reads one line from standard input. On a terminal it uses getpass, so a typed or pasted token isn't shown.
      An empty line, or one with spaces or control characters in it, is refused.
    • It checks the token with GET /api/v1/user. A 401 says "token refused" and a 403 names the missing scope. The
      token is never printed.
    • It saves the token with write_private, under the session lock, as forge.json:
      {"kind": "token", "login": ..., "access_token": ..., "expires_at": 4102444800}. There is no refresh token, so
      it never touches the OAuth grant. The far expires_at (2100-01-01) is for an older forge.py, which knows no kind
      and reads it before renewing: such a copy uses the token as an access token not yet due (checked against main's
      forge_session). This forge.py never renews a token, whatever the field says.
    • It then sets up git's credential helper and the forge remote with the same code the Discord sign-in runs.
    • It prints the scopes forge.py's commands need: write:repository, write:issue and read:user (below).
  • tools: forge_session returns a saved token as it is, never renewing it (magicbean_login.py).
    • forge_expire marks a token "refused": true instead of zeroing its expiry. It runs on git's credential erase,
      and now also when one of forge.py's API calls gets a 401 with a token (api in forge.py).
    • The next use then says the token was refused (deleted or expired) and to make a new one and run
      login --token-stdin. It never says "could not be renewed".
    • LoginError carries the HTTP status (status), which those two checks read.
  • tools: whoami adds "(personal access token)" for a token. logout works for both kinds; for a token it adds that
    the token stays valid on the site until deleted. Plain login keeps a saved token and says so. --again still
    signs in with Discord, replacing it.
  • tools: the team chat. matrix_from_forge, when the sign-in service refuses a token, says to use
    team_chat.py login --access-token-stdin, and that plain team_chat.py login (the Discord sign-in) would replace
    the token. forge.py's and team_chat.py's help say the same.
  • docs: docs/forge.md has a new "Two machines" section (the one-grant behaviour, the token route, the scopes, the
    team chat) and three new lines in "If something goes wrong". CONTRIBUTING.md's "Sign in" step points to it.
  • tests: tests/test_forge_token_login.py (new). It runs forge.py against a stand-in Forgejo on 127.0.0.1 and runs
    git in a sandbox (a temporary HOME, XDG_CONFIG_HOME and GIT_CONFIG_GLOBAL, no system config). It checks:
    • the token is checked, saved (mode 600) and used with no renewal, years later too;
    • forge.py's API calls send it, and the credential helper answers with it, also through a real
      git credential fill;
    • git is set up the same as for the Discord sign-in;
    • a 401, a 403, an empty line and two tokens on one line fail cleanly, print no token, and leave the saved sign-in
      and git untouched;
    • a token refused later says to sign in again;
    • whoami and logout work for both kinds;
    • the Discord path is unchanged (that test passes on main too);
    • the chat error names the other way in.
    • It fails on main.
  • Open question, @HumanBean: does /_magicbean/matrix-login take a personal access token? Its code isn't here.
    • If it asks Forgejo's GET /api/v1/user with the token, a token with read:user works.
    • If it uses /login/oauth/userinfo or /login/oauth/introspect, Forgejo v17.0.1 takes only its own OAuth tokens
      there, so it would refuse one. userinfo runs only the OAuth2 method (routers/web/web.go:431-439), which parses
      the token as a Forgejo-signed JWT (services/auth/method/oauth2.go:84); introspect parses it the same way
      (routers/web/auth/oauth.go:401).
    • The PR doesn't depend on the answer: the error points to team_chat.py login --access-token-stdin either way.
  • Left out on purpose: a guard in team_chat.py login against replacing a saved token. The help and the docs say not
    to run it on a token machine; refusing would change the chat's sign-in for everyone.
  • Left out: the alternatives for an admin are still open: turn off [oauth2] INVALIDATE_REFRESH_TOKENS (it weakens
    refresh-token reuse detection for everyone), or register a second OAuth application.

The scopes, from each API call forge.py and test_requests.py make (Forgejo v17.0.1 routers/api/v1/api.go;
write includes read):

Scope Calls
read:user GET /user: the check at sign-in, and fork, pr create, pr list --mine (the /user group, lines 689-798)
write:repository git push to your fork (routers/web/repo/githttp.go:156 checks the repository scope at the push's level); fork (POST /repos/.../forks); pr create, ready, wip, edit, update (POST/PATCH .../pulls); pr review (POST .../pulls/N/reviews); reads of pulls, .diff, branches, git/commits, your fork and collaborators/.../permission (test_requests.py) (the /repos/{owner}/{repo} group, lines 817-1131)
write:issue issue create, issue comment, pr comment, pr update (POST .../issues[/N/comments]); --attach (PATCH .../issues/comments/N, POST .../assets); pr wip --needs-measurements, pr ready (POST/DELETE .../issues/N/labels); reads of issues, comments and labels (the issues and labels group, lines 1143-1256)

Measurements

Verdict: works, game-free: the new test fails on main (8 of 9: --token-stdin unknown) and passes here (9 of 9),
2 runs each before the rebase and 1 after; the suite adds no failure. A tooling change: its test output is the
measurement (CONTRIBUTING's "build, tools" row).

Commits: main 6c9edffe5d (2026-11-14); this PR feat/forge-token-login @
f7b6a93d4e.
Hardware: Apple M4 Max, macOS 26.7.1, git 2.54.0 (Apple Git-157), Python 3.14.10. Game-free: no GPU, no game
Scene: none (a tooling change: CONTRIBUTING's "build, tools" row takes test output)
Method: tests/test_forge_token_login.py against main's scripts/ and against this PR. Before the rebase (main
5e072fc8, this PR a65a4f1e) interleaved: main, this PR, main, this PR; after it once each, on 6c9edffe's scripts/ and
at f7b6a93d. The full game-free suite on this PR, compared with main's baseline run on the same Mac
How to reproduce: python -I -B tests/test_forge_token_login.py -v (for main's side: copy it into a checkout of main's
tests/ and run it there)

Before: main @ 6c9edffe After: this PR @ f7b6a93d
tests/test_forge_token_login.py (2 runs each before the rebase, 1 after) FAIL: 8 of 9 (--token-stdin unknown); the Discord-path test passes PASS: 9 of 9
full game-free suite on macOS (tests/run_all.py --jobs 6) 531 passed, 32 failed, 17 skipped 532 passed, 32 failed, 17 skipped
main @ 6c9edffe:
test_discord_sign_in_unchanged ... ok
test_token_is_checked_saved_and_git_set_up ... FAIL
AssertionError: 2 != 0 : usage: test_forge_token_login.py [-h]
                                 {login,fork,whoami,logout,credential,pr,issue} ...
test_forge_token_login.py: error: unrecognized arguments: --token-stdin
(the other 7 fail on the same unknown option)
FAILED (failures=8)

this PR @ f7b6a93d:
Ran 9 tests
OK

Screenshots: none; nothing on screen.

Flags and settings

One new command-line option, forge.py login --token-stdin; no switch, menu row or ini key.

Flag What it is Before this PR After this PR Effect Helps / doesn't help / untested Recommendation
forge.py login --token-stdin Signs in with a personal access token from standard input instead of Discord - (no such option) Off: plain login is unchanged; used only when given The sign-in is never renewed, so it no longer signs your other machines out Helps a second machine on the same account; doesn't help on one machine (Discord is simpler there); untested on Windows and Linux Use it on every machine but one; keep Discord on the main one

Testing

Tested:

  • On an Apple M4 Max with macOS 26.7.1, game-free only, with no request to git.magicbean.dev: every request went to the
    test's stand-in on 127.0.0.1, and the tokens were made up.
  • tests/test_forge_token_login.py: 8 of its 9 tests fail on main, and all 9 pass here. The Discord-path test passes
    on both.
  • tests/test_forge_pr.py, tests/test_forge_session_lock.py, tests/test_team_chat.py and
    tests/test_check_pr_description.py pass, test_forge_pr and test_check_pr_description as #571 rewrote them.
  • The full game-free suite at f7b6a93d, compared with main 6c9edffe's by test name.
    • By test name, against main 6c9edffe's run on the same Mac: only one status changed: test_forge_token_login, absent
      on main, passes here.
    • These 32 checks fail here and on main on this Mac, by name (not triaged here: some need the macOS port's other pull
      requests, some are checks main added or changed since 5e072fc8): test_appimage_package_retry,
      test_appimage_player_build, test_collect_wide, test_control, test_cpm_discovery, test_crash_dialog,
      test_crash_record, test_env_on, test_game_layout, test_lift_cleanup_error, test_linux_preflight,
      test_longshot_ctl, test_low_memory_notice, test_make_game_cache, test_mem_gate, test_mem_gate_children,
      test_mirror_trim, test_oot_state_tap, test_pcache_header_read, test_pipeline_cache_file,
      test_pipeline_cache_save, test_pipeline_play_cache, test_pipeline_tour, test_prof_symbols,
      test_program_cache_cleanup, test_program_cache_one_variant, test_readback_late, test_report_run,
      test_split_giants, test_vk_pgo, test_vram_budget_row, test_walk_key.
    • The same 17 checks skipped as on main.
    • Crash reports during the run: check.exe-2026-11-14-192825.ips, recorder_sync_test.exe-2026-11-14-192647.ips (main's
      run: fault-arm64-2026-11-14-180704.ips, recorder_sync_test.exe-2026-11-14-180744.ips).
  • In a terminal (a pseudo-terminal driven by a script): read_token prompts "Personal access token for
    git.magicbean.dev (not shown):", reads the line, and the typed text does not come back in the output.
  • forge.py login --help shows --token-stdin with the token page and the scopes.
  • After the rebase onto main aa95d35f, at 8e04fc23 (f7b6a93d before the rebases onto e090cc48 and aa95d35f):
    Sweep3's checks of this head (the C/C++ it changes compiled, and its own tests run, on arm64 and x86_64 where they
    apply) gave the same results as before; python -I -B tests/run_all.py on wave S1's merge 11a5b4e7, which carries
    this head: 568/22/17 (passed/failed/skipped) against main aa95d35f's 543/33/17 on this Mac, no new failure
    (reports/2026-11-15-rebase-sweep-3.md).

Not tested:

  • A real token against git.magicbean.dev: nothing here talked to the server. The first real use is the author's own
    sign-in on the Mac (this PR's second machine), with whoami, a fetch and a push to his fork; its result goes in a
    comment.
  • Whether the team's sign-in service (/_magicbean/matrix-login) takes a personal access token: its code isn't in
    this repository (the open question under What changed). Either way the chat is reached: the error names
    --access-token-stdin.
  • Windows and Linux: neither the test nor the option has run there. getpass reads the console on Windows and the
    terminal on Linux, and the sign-in is saved in each OS's config folder.

Must be tested:

  • Windows (a console) and Linux (a terminal), no GPU needed: on a checkout of this PR (head 8e04fc23), run
    python -I -B tests/test_forge_token_login.py -v; make a personal access token at
    git.magicbean.dev/user/settings/applications with write:repository, write:issue and read:user, run
    python -I scripts/forge.py login --token-stdin and type or paste it, then whoami, git fetch forge and a push
    to your fork; on Linux also pipe it in (... --token-stdin < file); expecting the test to pass 9 of 9 (main's
    forge.py has no --token-stdin), the token not shown as it is typed or pasted, whoami naming you with
    "(personal access token)", and the fetch and the push signing in through it with no prompt; forge.py login --again
    signs back in with Discord afterwards (by hand: test request T-KP6C)

Risks

What could break: low. Plain login and --again, the OAuth renewal, forge_expire for an OAuth sign-in, and the
git setup are unchanged; the test's Discord-path part passes on main and here.

  • A token is a long-lived secret in forge.json, which only you can read, as the OAuth sign-in already is. It stays
    valid until it is deleted on the site or expires; logout says so.
  • The token's scopes can't be checked from the API. login prints the ones it needs, and a missing one shows as
    Forgejo's 403 on the command that needs it.
  • Landing order: rebased on main 6c9edffe (2026-11-14) without a conflict. Main's #571 (pull request descriptions) had
    changed scripts/forge.py, docs/forge.md and CONTRIBUTING.md in other places, and #543 CONTRIBUTING.md too;
    this PR's hunks applied as they were. No conflict with the author's other open PRs as rebased on 6c9edffe
    (git merge-tree of the heads, 2026-11-14).

Turning it off: don't pass --token-stdin (plain login is unchanged), or revert the commits. A machine signed in
with a token then needs forge.py login again.

Compatibility: the builder's tools: forge.json can now hold a token sign-in ("kind": "token"), with a far
expires_at so that an older forge.py uses it as it is. Nothing changes for an OAuth sign-in, saves, the options file,
the caches or the vk ABI.

Issues

Fixes #435

Checklist

Everyone

  • One topic.
  • The title is a Conventional Commit (type(scope): description) of the status line's type.
  • Why gives the reason plainly and links the issues that fit (the tracker searched).
  • Measurements has a verdict, or one line saying why there is nothing to measure.
  • Testing names what was tested, what wasn't, and what must still be tested, where and how.
  • Rebased on current main: on aa95d35f (#761, 2026-11-15), head 8e04fc23, without a conflict; this PR's own
    commits, old → new: 294aed28 → eba8f97c, a71f15ae → 93cc757a, f7b6a93d → 8e04fc23; the commit names
    elsewhere in this description are from before the rebase.
  • python -I -B tests/run_all.py passes (any failure that also fails on main is named). On macOS: 532 passed, 17
    skipped, and 32 failures that also fail on main 6c9edffe on this Mac, named under Testing. After the rebase onto main
    aa95d35f (head 8e04fc23): wave S1's merge 11a5b4e7, 568/22/17 against main aa95d35f's 543/33/17, no new failure.
  • No game files, dumps, build output or generated files.
  • third_party/ untouched (a new folder with a PROVENANCE.md instead).
  • The license is OK: GPL-3.0-or-later, nothing incompatible and nothing decompiled.

When it applies

  • AI assistance recorded with Assisted-by: / Co-Authored-By: trailers.
  • A game-free test added for the fix, failing on main and passing here.
  • Docs updated.
**Type:** feat · **Risk:** low · **Tested on:** macOS 26.7.1, Apple M4 Max (game-free checks against a stand-in Forgejo, no game run) · **Needs:** Windows and Linux, no GPU needed (a real token on git.magicbean.dev) ## Why Signing in with `python -I scripts/forge.py login` on a second machine signs you out on the first within about an hour, and signing in again there does the same to the second. Someone who works on two machines can't stay signed in on both. Fixes #435. Forgejo keeps **one OAuth grant per user per application**, and every machine signs in through the same app (`GIT_CLIENT_ID` in `scripts/magicbean_login.py`). In Forgejo's `routers/web/auth/oauth.go` (v17.0.1, the version git.magicbean.dev reports): - a sign-in reuses the user's grant for the app (`app.GetGrantByUserID`, line 617); - every token issued, at a sign-in or a refresh, increments the grant's counter when `InvalidateRefreshTokens` is on (lines 161-162); - a refresh token carrying an older count is refused with `unauthorized_client`, "token was already used" (lines 827-831). git.magicbean.dev has `InvalidateRefreshTokens` on. So when machine A signs in or renews, machine B's refresh token goes stale; B works until its access token runs out (about an hour), then its renewal fails: ``` the git.magicbean.dev sign-in could not be renewed (POST https://git.magicbean.dev/login/oauth/access_token: HTTP 400 {"error":"unauthorized_client","error_description":"token was already used"}): sign in again ``` The `_SessionLock` fixed this race between two processes on one machine; it can't help across machines. ## What changed `forge.py login --token-stdin` signs in with a Forgejo personal access token instead of Discord. The token has no refresh token and is never renewed, so it leaves the one OAuth grant alone and no machine signs another out; when Forgejo refuses it, forge.py says to make a new one. Plain `login`, the Discord sign-in and its renewal are unchanged. - Read first: `token_sign_in`, `forge_session` and `forge_expire` in `magicbean_login.py`, then `cmd_login` and `api` in `forge.py`. - tools: `python -I scripts/forge.py login --token-stdin` (`cmd_login` and `read_token` in `scripts/forge.py`, `token_sign_in` in `scripts/magicbean_login.py`). - It reads one line from standard input. On a terminal it uses `getpass`, so a typed or pasted token isn't shown. An empty line, or one with spaces or control characters in it, is refused. - It checks the token with `GET /api/v1/user`. A 401 says "token refused" and a 403 names the missing scope. The token is never printed. - It saves the token with `write_private`, under the session lock, as `forge.json`: `{"kind": "token", "login": ..., "access_token": ..., "expires_at": 4102444800}`. There is no refresh token, so it never touches the OAuth grant. The far `expires_at` (2100-01-01) is for an older forge.py, which knows no kind and reads it before renewing: such a copy uses the token as an access token not yet due (checked against main's `forge_session`). This forge.py never renews a token, whatever the field says. - It then sets up git's credential helper and the `forge` remote with the same code the Discord sign-in runs. - It prints the scopes forge.py's commands need: `write:repository`, `write:issue` and `read:user` (below). - tools: `forge_session` returns a saved token as it is, never renewing it (`magicbean_login.py`). - `forge_expire` marks a token `"refused": true` instead of zeroing its expiry. It runs on git's `credential erase`, and now also when one of forge.py's API calls gets a 401 with a token (`api` in `forge.py`). - The next use then says the token was refused (deleted or expired) and to make a new one and run `login --token-stdin`. It never says "could not be renewed". - `LoginError` carries the HTTP status (`status`), which those two checks read. - tools: `whoami` adds "(personal access token)" for a token. `logout` works for both kinds; for a token it adds that the token stays valid on the site until deleted. Plain `login` keeps a saved token and says so. `--again` still signs in with Discord, replacing it. - tools: the team chat. `matrix_from_forge`, when the sign-in service refuses a token, says to use `team_chat.py login --access-token-stdin`, and that plain `team_chat.py login` (the Discord sign-in) would replace the token. forge.py's and team_chat.py's help say the same. - docs: `docs/forge.md` has a new "Two machines" section (the one-grant behaviour, the token route, the scopes, the team chat) and three new lines in "If something goes wrong". CONTRIBUTING.md's "Sign in" step points to it. - tests: `tests/test_forge_token_login.py` (new). It runs forge.py against a stand-in Forgejo on 127.0.0.1 and runs git in a sandbox (a temporary HOME, XDG_CONFIG_HOME and GIT_CONFIG_GLOBAL, no system config). It checks: - the token is checked, saved (mode 600) and used with no renewal, years later too; - forge.py's API calls send it, and the credential helper answers with it, also through a real `git credential fill`; - git is set up the same as for the Discord sign-in; - a 401, a 403, an empty line and two tokens on one line fail cleanly, print no token, and leave the saved sign-in and git untouched; - a token refused later says to sign in again; - whoami and logout work for both kinds; - the Discord path is unchanged (that test passes on main too); - the chat error names the other way in. - It fails on main. - Open question, @HumanBean: does `/_magicbean/matrix-login` take a personal access token? Its code isn't here. - If it asks Forgejo's `GET /api/v1/user` with the token, a token with `read:user` works. - If it uses `/login/oauth/userinfo` or `/login/oauth/introspect`, Forgejo v17.0.1 takes only its own OAuth tokens there, so it would refuse one. userinfo runs only the OAuth2 method (`routers/web/web.go:431-439`), which parses the token as a Forgejo-signed JWT (`services/auth/method/oauth2.go:84`); introspect parses it the same way (`routers/web/auth/oauth.go:401`). - The PR doesn't depend on the answer: the error points to `team_chat.py login --access-token-stdin` either way. - Left out on purpose: a guard in `team_chat.py login` against replacing a saved token. The help and the docs say not to run it on a token machine; refusing would change the chat's sign-in for everyone. - Left out: the alternatives for an admin are still open: turn off `[oauth2] INVALIDATE_REFRESH_TOKENS` (it weakens refresh-token reuse detection for everyone), or register a second OAuth application. The scopes, from each API call forge.py and `test_requests.py` make (Forgejo v17.0.1 `routers/api/v1/api.go`; `write` includes `read`): | Scope | Calls | |---|---| | `read:user` | `GET /user`: the check at sign-in, and `fork`, `pr create`, `pr list --mine` (the `/user` group, lines 689-798) | | `write:repository` | `git push` to your fork (`routers/web/repo/githttp.go:156` checks the repository scope at the push's level); `fork` (`POST /repos/.../forks`); `pr create`, `ready`, `wip`, `edit`, `update` (`POST`/`PATCH .../pulls`); `pr review` (`POST .../pulls/N/reviews`); reads of pulls, `.diff`, branches, `git/commits`, your fork and `collaborators/.../permission` (`test_requests.py`) (the `/repos/{owner}/{repo}` group, lines 817-1131) | | `write:issue` | `issue create`, `issue comment`, `pr comment`, `pr update` (`POST .../issues[/N/comments]`); `--attach` (`PATCH .../issues/comments/N`, `POST .../assets`); `pr wip --needs-measurements`, `pr ready` (`POST`/`DELETE .../issues/N/labels`); reads of issues, comments and labels (the issues and labels group, lines 1143-1256) | ## Measurements **Verdict:** works, game-free: the new test fails on main (8 of 9: `--token-stdin` unknown) and passes here (9 of 9), 2 runs each before the rebase and 1 after; the suite adds no failure. A tooling change: its test output is the measurement (CONTRIBUTING's "build, tools" row). Commits: main 6c9edffe5da1f5b5383977aeab3f544c0de25d4c (2026-11-14); this PR feat/forge-token-login @ f7b6a93d4ed83290641c0604550ab10ae6973b30. Hardware: Apple M4 Max, macOS 26.7.1, git 2.54.0 (Apple Git-157), Python 3.14.10. Game-free: no GPU, no game Scene: none (a tooling change: CONTRIBUTING's "build, tools" row takes test output) Method: `tests/test_forge_token_login.py` against main's `scripts/` and against this PR. Before the rebase (main 5e072fc8, this PR a65a4f1e) interleaved: main, this PR, main, this PR; after it once each, on 6c9edffe's `scripts/` and at f7b6a93d. The full game-free suite on this PR, compared with main's baseline run on the same Mac How to reproduce: `python -I -B tests/test_forge_token_login.py -v` (for main's side: copy it into a checkout of main's `tests/` and run it there) | | Before: main @ 6c9edffe | After: this PR @ f7b6a93d | |---|---|---| | `tests/test_forge_token_login.py` (2 runs each before the rebase, 1 after) | FAIL: 8 of 9 (`--token-stdin` unknown); the Discord-path test passes | PASS: 9 of 9 | | full game-free suite on macOS (`tests/run_all.py --jobs 6`) | 531 passed, 32 failed, 17 skipped | 532 passed, 32 failed, 17 skipped | ``` main @ 6c9edffe: test_discord_sign_in_unchanged ... ok test_token_is_checked_saved_and_git_set_up ... FAIL AssertionError: 2 != 0 : usage: test_forge_token_login.py [-h] {login,fork,whoami,logout,credential,pr,issue} ... test_forge_token_login.py: error: unrecognized arguments: --token-stdin (the other 7 fail on the same unknown option) FAILED (failures=8) this PR @ f7b6a93d: Ran 9 tests OK ``` Screenshots: none; nothing on screen. ### Flags and settings One new command-line option, `forge.py login --token-stdin`; no switch, menu row or ini key. | Flag | What it is | Before this PR | After this PR | Effect | Helps / doesn't help / untested | Recommendation | |---|---|---|---|---|---|---| | `forge.py login --token-stdin` | Signs in with a personal access token from standard input instead of Discord | - (no such option) | Off: plain `login` is unchanged; used only when given | The sign-in is never renewed, so it no longer signs your other machines out | Helps a second machine on the same account; doesn't help on one machine (Discord is simpler there); untested on Windows and Linux | Use it on every machine but one; keep Discord on the main one | ## Testing **Tested:** - On an Apple M4 Max with macOS 26.7.1, game-free only, with no request to git.magicbean.dev: every request went to the test's stand-in on 127.0.0.1, and the tokens were made up. - `tests/test_forge_token_login.py`: 8 of its 9 tests fail on main, and all 9 pass here. The Discord-path test passes on both. - `tests/test_forge_pr.py`, `tests/test_forge_session_lock.py`, `tests/test_team_chat.py` and `tests/test_check_pr_description.py` pass, `test_forge_pr` and `test_check_pr_description` as #571 rewrote them. - The full game-free suite at f7b6a93d, compared with main 6c9edffe's by test name. - By test name, against main 6c9edffe's run on the same Mac: only one status changed: `test_forge_token_login`, absent on main, passes here. - These 32 checks fail here and on main on this Mac, by name (not triaged here: some need the macOS port's other pull requests, some are checks main added or changed since 5e072fc8): `test_appimage_package_retry`, `test_appimage_player_build`, `test_collect_wide`, `test_control`, `test_cpm_discovery`, `test_crash_dialog`, `test_crash_record`, `test_env_on`, `test_game_layout`, `test_lift_cleanup_error`, `test_linux_preflight`, `test_longshot_ctl`, `test_low_memory_notice`, `test_make_game_cache`, `test_mem_gate`, `test_mem_gate_children`, `test_mirror_trim`, `test_oot_state_tap`, `test_pcache_header_read`, `test_pipeline_cache_file`, `test_pipeline_cache_save`, `test_pipeline_play_cache`, `test_pipeline_tour`, `test_prof_symbols`, `test_program_cache_cleanup`, `test_program_cache_one_variant`, `test_readback_late`, `test_report_run`, `test_split_giants`, `test_vk_pgo`, `test_vram_budget_row`, `test_walk_key`. - The same 17 checks skipped as on main. - Crash reports during the run: check.exe-2026-11-14-192825.ips, recorder_sync_test.exe-2026-11-14-192647.ips (main's run: fault-arm64-2026-11-14-180704.ips, recorder_sync_test.exe-2026-11-14-180744.ips). - In a terminal (a pseudo-terminal driven by a script): `read_token` prompts "Personal access token for git.magicbean.dev (not shown):", reads the line, and the typed text does not come back in the output. - `forge.py login --help` shows `--token-stdin` with the token page and the scopes. - **After the rebase onto main aa95d35f**, at `8e04fc23` (`f7b6a93d` before the rebases onto e090cc48 and aa95d35f): Sweep3's checks of this head (the C/C++ it changes compiled, and its own tests run, on arm64 and x86_64 where they apply) gave the same results as before; `python -I -B tests/run_all.py` on wave S1's merge `11a5b4e7`, which carries this head: 568/22/17 (passed/failed/skipped) against main aa95d35f's 543/33/17 on this Mac, no new failure (`reports/2026-11-15-rebase-sweep-3.md`). **Not tested:** - A real token against git.magicbean.dev: nothing here talked to the server. The first real use is the author's own sign-in on the Mac (this PR's second machine), with `whoami`, a fetch and a push to his fork; its result goes in a comment. - Whether the team's sign-in service (`/_magicbean/matrix-login`) takes a personal access token: its code isn't in this repository (the open question under What changed). Either way the chat is reached: the error names `--access-token-stdin`. - Windows and Linux: neither the test nor the option has run there. `getpass` reads the console on Windows and the terminal on Linux, and the sign-in is saved in each OS's config folder. **Must be tested:** - Windows (a console) and Linux (a terminal), no GPU needed: on a checkout of this PR (head 8e04fc23), run `python -I -B tests/test_forge_token_login.py -v`; make a personal access token at git.magicbean.dev/user/settings/applications with write:repository, write:issue and read:user, run `python -I scripts/forge.py login --token-stdin` and type or paste it, then `whoami`, `git fetch forge` and a push to your fork; on Linux also pipe it in (`... --token-stdin < file`); expecting the test to pass 9 of 9 (main's forge.py has no `--token-stdin`), the token not shown as it is typed or pasted, `whoami` naming you with "(personal access token)", and the fetch and the push signing in through it with no prompt; `forge.py login --again` signs back in with Discord afterwards (by hand: test request T-KP6C) ## Risks **What could break:** low. Plain `login` and `--again`, the OAuth renewal, `forge_expire` for an OAuth sign-in, and the git setup are unchanged; the test's Discord-path part passes on main and here. - A token is a long-lived secret in `forge.json`, which only you can read, as the OAuth sign-in already is. It stays valid until it is deleted on the site or expires; `logout` says so. - The token's scopes can't be checked from the API. `login` prints the ones it needs, and a missing one shows as Forgejo's 403 on the command that needs it. - Landing order: rebased on main 6c9edffe (2026-11-14) without a conflict. Main's #571 (pull request descriptions) had changed `scripts/forge.py`, `docs/forge.md` and `CONTRIBUTING.md` in other places, and #543 `CONTRIBUTING.md` too; this PR's hunks applied as they were. No conflict with the author's other open PRs as rebased on 6c9edffe (`git merge-tree` of the heads, 2026-11-14). **Turning it off:** don't pass `--token-stdin` (plain `login` is unchanged), or revert the commits. A machine signed in with a token then needs `forge.py login` again. **Compatibility:** the builder's tools: `forge.json` can now hold a token sign-in (`"kind": "token"`), with a far `expires_at` so that an older forge.py uses it as it is. Nothing changes for an OAuth sign-in, saves, the options file, the caches or the vk ABI. ## Issues Fixes #435 ## Checklist ### Everyone - [x] One topic. - [x] The title is a Conventional Commit (`type(scope): description`) of the status line's type. - [x] Why gives the reason plainly and links the issues that fit (the tracker searched). - [x] Measurements has a verdict, or one line saying why there is nothing to measure. - [x] Testing names what was tested, what wasn't, and what must still be tested, where and how. - [x] Rebased on current `main`: on aa95d35f (#761, 2026-11-15), head `8e04fc23`, without a conflict; this PR's own commits, old → new: `294aed28` → `eba8f97c`, `a71f15ae` → `93cc757a`, `f7b6a93d` → `8e04fc23`; the commit names elsewhere in this description are from before the rebase. - [x] `python -I -B tests/run_all.py` passes (any failure that also fails on `main` is named). On macOS: 532 passed, 17 skipped, and 32 failures that also fail on main 6c9edffe on this Mac, named under Testing. After the rebase onto main aa95d35f (head `8e04fc23`): wave S1's merge `11a5b4e7`, 568/22/17 against main aa95d35f's 543/33/17, no new failure. - [x] No game files, dumps, build output or generated files. - [x] `third_party/` untouched (a new folder with a `PROVENANCE.md` instead). - [x] The license is OK: GPL-3.0-or-later, nothing incompatible and nothing decompiled. ### When it applies - [x] AI assistance recorded with `Assisted-by:` / `Co-Authored-By:` trailers. - [x] A game-free test added for the fix, failing on `main` and passing here. - [x] Docs updated.
Forgejo keeps one OAuth grant per person and application, and every token it issues, at a sign-in or a renewal, moves
the grant's counter on; a refresh token from an older count is refused as "token was already used"
(routers/web/auth/oauth.go 161-162, 617, 827-831 in Forgejo v17.0.1). Every machine signs in through the same app
(GIT_CLIENT_ID), so two machines on one account sign each other out within about an hour.

login --token-stdin reads a Forgejo personal access token from standard input (not echoed on a terminal), checks it
with GET /api/v1/user ("token refused" on a 401, the missing scope on a 403, never printing it) and saves it with
write_private as forge.json {"kind": "token", "login", "access_token"}: no refresh token, no expiry. forge_session
returns it as it is; forge_expire (git's erase, or a 401 from forge.py's API calls) marks it refused, and the next use
says to make a new one and sign in again instead of "could not be renewed". The git credential helper and the forge
remote are set up as the Discord sign-in sets them up. whoami names the kind, logout says the token itself stays valid
on the site, plain login keeps a saved token and --again goes back to Discord. The scopes forge.py's calls need are
printed: write:repository, write:issue, read:user.

The team chat's sign-in service is not in this repository, so whether it takes a personal access token can't be read
from the code; when it refuses one, the error, forge.py's help, team_chat.py's help and docs/forge.md point to
team_chat.py login --access-token-stdin, and say that plain team_chat.py login (the Discord sign-in) would replace the
token. docs/forge.md has a "Two machines" section; CONTRIBUTING.md's sign-in step points to it.

tests/test_forge_token_login.py (new) runs forge.py against a stand-in Forgejo on 127.0.0.1, with git sandboxed in a
temporary HOME and GIT_CONFIG_GLOBAL. It fails on main; its Discord-path test passes on both.

Fixes #435

Assisted-by: Claude Opus 5.5
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Assisted-by: Claude Opus 5.5
Co-Authored-By: Claude Opus 5.5 <[email protected]>
fix(tools): a personal access token is saved with a far expiry, so an older forge.py uses it as it is
All checks were successful
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Has been skipped
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Has been skipped
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Has been skipped
Third-party parts / The same as upstream (pull_request) Has been skipped
PR description / Description follows the template (pull_request_target) Successful in 4s
CI / Conventional Commit title (pull_request) Successful in 3s
CI / Workflow lint (actionlint) (pull_request) Successful in 7s
Third-party parts / As their notes say (pull_request) Successful in 30s
Website / Website on Cloudflare (pull_request_target) Has been skipped
cf6fa5e943
forge.py from before --token-stdin reads expires_at before it renews a sign-in and has no other field for the
kind, so a token saved without one stopped it with a KeyError. The token is now saved with 2100-01-01: older copies
use it as an access token that is not yet due, and this forge.py, which knows the kind, still never renews it.
Checked against main 5e072fc8's magicbean_login.forge_session.

Assisted-by: Claude Opus 5.5
Co-Authored-By: Claude Opus 5.5 <[email protected]>
mooki force-pushed feat/forge-token-login from cf6fa5e943
All checks were successful
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Has been skipped
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Has been skipped
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Has been skipped
Third-party parts / The same as upstream (pull_request) Has been skipped
PR description / Description follows the template (pull_request_target) Successful in 4s
CI / Conventional Commit title (pull_request) Successful in 3s
CI / Workflow lint (actionlint) (pull_request) Successful in 7s
Third-party parts / As their notes say (pull_request) Successful in 30s
Website / Website on Cloudflare (pull_request_target) Has been skipped
to f7b6a93d4e
All checks were successful
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Has been skipped
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Has been skipped
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Has been skipped
Third-party parts / The same as upstream (pull_request) Has been skipped
Website / Website on Cloudflare (pull_request) Has been skipped
CI / Conventional Commit title (pull_request) Successful in 4s
CI / Workflow lint (actionlint) (pull_request) Successful in 6s
Third-party parts / As their notes say (pull_request) Successful in 11s
PR description / Description follows the template (pull_request_target) Successful in 4s
2026-11-15 01:52:10 +00:00
Compare
mooki changed title from WIP: feat(tools): forge.py login --token-stdin, a personal access token for a second machine to feat(tools): forge.py login --token-stdin, a personal access token for a second machine 2026-11-15 05:25:42 +00:00
mooki force-pushed feat/forge-token-login from f7b6a93d4e
All checks were successful
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Has been skipped
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Has been skipped
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Has been skipped
Third-party parts / The same as upstream (pull_request) Has been skipped
Website / Website on Cloudflare (pull_request) Has been skipped
CI / Conventional Commit title (pull_request) Successful in 4s
CI / Workflow lint (actionlint) (pull_request) Successful in 6s
Third-party parts / As their notes say (pull_request) Successful in 11s
PR description / Description follows the template (pull_request_target) Successful in 4s
to 4b1dc3c453
All checks were successful
PR description / Description follows the template (pull_request_target) Successful in 16s
2026-11-15 07:14:02 +00:00
Compare
mooki force-pushed feat/forge-token-login from 4b1dc3c453
All checks were successful
PR description / Description follows the template (pull_request_target) Successful in 16s
to 8e04fc239b
Some checks failed
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Has been skipped
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Has been skipped
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Has been skipped
Third-party parts / The same as upstream (pull_request) Has been skipped
Website / Website on Cloudflare (pull_request) Has been skipped
CI / Conventional Commit title (pull_request) Successful in 4s
CI / Workflow lint (actionlint) (pull_request) Successful in 7s
Guide screenshots / Guide images and local UI source review (pull_request) Failing after 7s
Third-party parts / As their notes say (pull_request) Successful in 9s
PR description / Description follows the template (pull_request_target) Successful in 4s
2026-11-15 13:05:40 +00:00
Compare
Owner

Thanks for this, and for tracking the cause down in #435. We took the server-side route you suggested there: git.magicbean.dev has [oauth2] INVALIDATE_REFRESH_TOKENS = false since 2026-11-15, so each machine keeps its own Discord sign-in and a renewal on one no longer signs the others out (checked: two copies of one sign-in both renewed from the same refresh token). That makes a personal-access-token sign-in unneeded for a second machine, so I am closing this. If a sign-in without Discord turns out to be needed (a headless box, a CI machine), reopen it.

Thanks for this, and for tracking the cause down in [#435](https://git.magicbean.dev/MagicBeanDev/Longshot/issues/435). We took the server-side route you suggested there: git.magicbean.dev has `[oauth2] INVALIDATE_REFRESH_TOKENS = false` since 2026-11-15, so each machine keeps its own Discord sign-in and a renewal on one no longer signs the others out (checked: two copies of one sign-in both renewed from the same refresh token). That makes a personal-access-token sign-in unneeded for a second machine, so I am closing this. If a sign-in without Discord turns out to be needed (a headless box, a CI machine), reopen it.
HumanBean closed this pull request 2026-11-15 18:09:57 +00:00
Some checks failed
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Has been skipped
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Has been skipped
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Has been skipped
Third-party parts / The same as upstream (pull_request) Has been skipped
Website / Website on Cloudflare (pull_request) Has been skipped
CI / Conventional Commit title (pull_request) Successful in 4s
CI / Workflow lint (actionlint) (pull_request) Successful in 7s
Guide screenshots / Guide images and local UI source review (pull_request) Failing after 7s
Third-party parts / As their notes say (pull_request) Successful in 9s
PR description / Description follows the template (pull_request_target) Successful in 4s

Pull request closed

Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
MagicBeanDev/Longshot!473
No description provided.