Merge pull request 'claude-code: the node side never writes a lease over a login the manager was not handed, and only a newer lineage over a handed one' (#712) from fix/licence-file-the-cli-accepts into main

This commit was merged in pull request #712.
This commit is contained in:
2026-11-20 13:24:15 +00:00
3 changed files with 146 additions and 14 deletions
+36 -2
View File
@@ -24,6 +24,7 @@ import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
@@ -143,6 +144,7 @@ func HomeScopeRefused(p Paths, target string) string {
func (p Paths) credentials() string { return filepath.Join(p.Home, ".claude", ".credentials.json") }
func (p Paths) account() string { return filepath.Join(p.Home, ".claude.json") }
func (p Paths) binding() string { return filepath.Join(p.State, "licence.json") }
func (p Paths) handed() string { return filepath.Join(p.State, "login-handed") }
func (p Paths) apiKey() string { return filepath.Join(p.keyDir(), "api-key") }
func (p Paths) helper() string { return filepath.Join(p.keyDir(), "api-key-helper") }
@@ -415,9 +417,26 @@ func GrantFor(p Paths, managerPublicKey string) (GrantAnswer, error) {
if err != nil {
return GrantAnswer{}, err
}
// Which login was handed: only its successor may replace it (ADR 0206, ADR 0209). The fingerprint, never
// the token.
if err := fsys.MkdirAll(p.State, 0o700); err != nil {
return GrantAnswer{}, err
}
if err := fsys.WriteFile(p.handed(), []byte(Fingerprint(rt)+"\n"), 0o600); err != nil {
return GrantAnswer{}, err
}
return GrantAnswer{Sealed: &box, Identity: ReadIdentity(p.account()), Fingerprint: Fingerprint(rt)}, nil
}
// readHanded is the fingerprint of the login last handed to the manager, or "".
func readHanded(p Paths) string {
raw, err := fsys.ReadFile(p.handed())
if err != nil {
return ""
}
return strings.TrimSpace(string(raw))
}
// Pull asks the seat which licence this node holds, then the vault for that licence's access secret, sealed to this
// module's key, and applies it (§19).
func Pull(p Paths, ask Ask, write WriteManaged) (map[string]any, error) {
@@ -606,8 +625,23 @@ func Apply(p Paths, c Current, sealed SealedBox, write WriteManaged) (map[string
return nil, err
}
local := ReadCredentials(p.credentials())
// A login waiting here was handed to the manager first (ADR 0206): what comes back is its successor,
// and the refresh token in the file is the one the manager just spent.
// A login made here that the manager has not adopted yet stays whole: the grant handed is the old
// lineage's, and writing it would spend the person's login unseen (ADR 0206, ADR 0209). The manager
// asks claude_code_grant for it on this node's report; its successor then comes as a newer generation.
// This return skips the binding's generation below on purpose: later binding events pull again until
// the manager adopts the login.
if LoginNotHanded(local, readHanded(p)) {
log.Printf("login kept: the login in the credentials file was not handed to the manager yet (licence %s)", c.Licence)
return map[string]any{"applied": false, "licence": c.Licence, "reason": LoginKept}, nil
}
// A login handed to the manager is replaced only by a grant of a newer lineage (its refresh token's
// expiry later than the login's, as the manager marks it): a lease older than the login is never
// written, so the manager's failed refresh is a finding on the manager, never a lost login here
// (H-A-138, hq design/47-H1-answers-59 175ed320).
if HoldsLogin(local) && !NewerLineage(local, g) {
log.Printf("login kept: the grant offered for licence %s is not of a newer lineage than the login in the credentials file", c.Licence)
return map[string]any{"applied": false, "licence": c.Licence, "reason": LoginKept}, nil
}
d := DecideApply(GrantOf(local), g, switched || HoldsLogin(local))
if d.Apply {
next := WithGrant(local, g)