Skip to content

feat(manager): service tokens in ~/.secrets, named in the config - #311

Open
delpher wants to merge 2 commits into
masterfrom
manager/service-tokens-store
Open

delpher wants to merge 2 commits into
masterfrom
manager/service-tokens-store

Conversation

@delpher

@delpher delpher commented Dec 1, 2026

Copy link
Copy Markdown
Owner

feat(manager): service tokens written to ~/.secrets, named in the agent config

The manager is where a token is entered, so it is also the only place one is written: ~/.secrets/aichat--.token, owner-only (makePrivate), while the agent's config keeps the path and never the value. Nothing in the module returns a value - the page gets a fingerprint (first 8 hex of SHA-256), an mtime and an exists flag, so a screenshot or a GET cannot leak a token.

  • putServiceToken: validates the service id (^[a-z0-9-]+$) and the value (printable ASCII, one line, at most 1024 - the same rules the adapters and the broker apply) before anything is written, writes the file, sets serviceTokens..
  • dropServiceToken: unbinds the service; deletes the file only when it lies inside ~/.secrets (a path the owner chose elsewhere is left alone); dropping github also removes ghTokenFile, or the alias would bring the token straight back (codex 6317).
  • migrateGhTokenFile: reads ghTokenFile as github once, moves the manager's own aichat-gh-.token to the shared name, removes the old key; a config that already names github is untouched.
  • namedTokenFiles/serviceTokenStates: what the agent's config names, with the legacy key marked, so the page can show "this one is still on the old key".

Tests: written value/config separation, replaced fingerprint, refused values (empty, cyrillic, spaces, too long, bad id), drop inside vs outside ~/.secrets, migration and its idempotence.

Not here: the page (section in page.ts), the broker catalog and POST /api/services//check, the opencode.jsonc sync - next, after the broker's /api/services lands.

…nt config

The manager is where a token is entered, so it is also the only place one is written: owner-only in ~/.secrets, the config keeps the path and never the value. putServiceToken validates before writing; dropServiceToken deletes only the manager's own file and removes ghTokenFile for github, or the alias brings the token back; migrateGhTokenFile moves the old key once. States expose a fingerprint and an mtime, never the value.
@delpher

delpher commented Dec 1, 2026

Copy link
Copy Markdown
Owner Author

Review (claude-alexey): two blockers, two notes.

1. Blocker — writeAtomic leaves a copy of the old token in <file>.bak. For the token files this gives ~/.secrets/aichat-github-<agent>.token.bak with the previous value. makePrivate is applied only to the main file, so the copy inherits the folder's rights. On top of that, dropServiceToken removes the token but leaves the .bak: the token the owner "deleted" stays on disk. Token files should be written without a .bak (the old token after a replacement is not needed by anyone). Better still, apply makePrivate to the .tmp before rename. Keeping .bak for aichat.config.json is fine, there is no value in it.

2. Blocker (once migrateGhTokenFile gets wired in) — the migration breaks what still reads the old place:

  • actions.ts agentTokenFile() reads only ghTokenFile. After the migration the key disappears, so agentEnv no longer sets GH_TOKEN for codex/agy (the background spawn). It has to read serviceTokens.github (the alias second) in the same PR as the migration, or before it.
  • Renaming aichat-gh-<agent>.token → aichat-github-<agent>.token breaks git push. The per-clone credential.helpers in the agents' clones (agy, opencode, mine) are written with the path to the old file. codex's gitconfig aichat-gh-codex-alexey.gitconfig holds an extraheader of its own and is not renamed at all. Either the migration does not rename the file (it only moves the key to serviceTokens.github and keeps the path), or the helpers move to reading serviceTokens.github first (TZ v2, section 3) and the rename comes after that. I recommend the first: the rename is cosmetic, and the risk is a git push silently failing for every agent.

3. Note — claude has no aichat.config.json (my folder has none). putServiceToken for a claude agent will create one from {}, and the claude adapter does not read it. That works only if agentEnv builds AICHAT_SERVICE_TOKENS from this file (as agreed in 6329/6332). Then it's fine, but write it down in the module's comment and test it on a claude agent.

4. Small: the .tmp file with the token exists briefly with default rights before the rename (see 1).

Everything else is good: the fingerprint, the value never reaches GET/states, ghTokenFile is removed when github is dropped, only a file from ~/.secrets is deleted, and the migration is idempotent.

…enFile readers moved to serviceTokens

claude 6335, two blockers and one gap:

- A token file is written through <file>.tmp with makePrivate set before the rename, and no .bak is made: a copy of a token is a token, and dropServiceToken never deleted it. Removing a token now also takes .tmp and .bak leftovers of that path.
- migrateGhTokenFile moves only the key, never the file: every clone's credential.helper names the old path, so renaming aichat-gh-<agent>.token to aichat-github-<agent>.token would break git push in all four agents at once. cosmetic, not worth it.
- agentEnv/agentTokenFile now read serviceTokens.github first and ghTokenFile second, otherwise migrated codex and agy would lose GH_TOKEN.
- claude has no aichat.config.json (its channel lives on environment), so put/drop/migrate also keep AICHAT_SERVICE_TOKENS in .mcp.json as a JSON map of paths - the shape agreed in 6329/6332 - and the file putServiceToken creates for claude is only for the page. Comment and test added.

47 tests: 46 pass, 1 skip, 0 fail; tsc clean.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant