claude-code: a login not handed gives way to a newer lease of its own account (H-A-140) #714

Merged
mesh-tools merged 1 commits from fix/licence-file-the-cli-accepts into main 2026-11-20 13:49:51 +00:00
2 changed files with 44 additions and 13 deletions
@@ -374,42 +374,56 @@ func TestALoginNotHandedIsNeverWrittenOverAndItsSuccessorReplacesIt(t *testing.T
offeredExp int64 // its access expiry, 0 for an hour from now
heard func(p Paths, ask Ask, w WriteManaged) (string, error)
want string // the access token the file holds after
account string // the email address the agent's account file names, "" for none
reason string // what the journal line says, "" for nothing
}{
{"the start's first view, a login made while stopped", login, false, 0, 0, func(p Paths, ask Ask, w WriteManaged) (string, error) {
return FirstBinding(p, ask, w)
}, "at-login"},
}, "at-login", "", ""},
{"binding.changed heard while it runs, before the grant was asked", login, false, 0, 0, func(p Paths, ask Ask, w WriteManaged) (string, error) {
return OnBindingChanged(p, []byte(`{"consumer":"laptop","licence":"personal","key":"personal","kind":"subscription","generation":9}`), ask, w)
}, "at-login"},
}, "at-login", "", ""},
{"the vault's change heard while it runs, before the grant was asked", login, false, 0, 0, func(p Paths, ask Ask, w WriteManaged) (string, error) {
return OnSecretChanged(p, []byte(`{"name":"licence.personal.access","generation":5}`), ask, w)
}, "at-login"},
}, "at-login", "", ""},
{"a login handed, met by its refresh: same lineage, later access expiry (H-A-139)", login, true, 9000, now + 3*7_200_000, func(p Paths, ask Ask, w WriteManaged) (string, error) {
return OnBindingChanged(p, []byte(`{"consumer":"laptop","licence":"personal","key":"personal","kind":"subscription","generation":9}`), ask, w)
}, "at-new"},
}, "at-new", "", ""},
{"a login handed, met by a newer lineage (H-A-138)", login, true, 200_000_000, 0, func(p Paths, ask Ask, w WriteManaged) (string, error) {
return OnBindingChanged(p, []byte(`{"consumer":"laptop","licence":"personal","key":"personal","kind":"subscription","generation":9}`), ask, w)
}, "at-new"},
}, "at-new", "", ""},
{"a login handed, met by the same lineage with an earlier access expiry (H-A-139)", login, true, 9000, 0, func(p Paths, ask Ask, w WriteManaged) (string, error) {
return OnBindingChanged(p, []byte(`{"consumer":"laptop","licence":"personal","key":"personal","kind":"subscription","generation":9}`), ask, w)
}, "at-login"},
}, "at-login", "", ""},
{"a login handed, met by the same lineage with an equal access expiry (H-A-139)", login, true, 9000, now + 7_200_000, func(p Paths, ask Ask, w WriteManaged) (string, error) {
return OnBindingChanged(p, []byte(`{"consumer":"laptop","licence":"personal","key":"personal","kind":"subscription","generation":9}`), ask, w)
}, "at-login"},
}, "at-login", "", ""},
{"a login handed, met by an older lineage (H-A-138)", login, true, -100_000_000, now + 3*7_200_000, func(p Paths, ask Ask, w WriteManaged) (string, error) {
return OnBindingChanged(p, []byte(`{"consumer":"laptop","licence":"personal","key":"personal","kind":"subscription","generation":9}`), ask, w)
}, "at-login"},
}, "at-login", "", ""},
{"a login handed, met by a lease with no lineage mark (H-A-138)", login, true, 0, now + 3*7_200_000, func(p Paths, ask Ask, w WriteManaged) (string, error) {
return OnBindingChanged(p, []byte(`{"consumer":"laptop","licence":"personal","key":"personal","kind":"subscription","generation":9}`), ask, w)
}, "at-login"},
}, "at-login", "", ""},
{"not handed, the same account, a lease that succeeds it: the latest login wins (H-A-140)", login, false, 9000, now + 3*7_200_000, func(p Paths, ask Ask, w WriteManaged) (string, error) {
return OnBindingChanged(p, []byte(`{"consumer":"laptop","licence":"personal","key":"personal","kind":"subscription","generation":9}`), ask, w)
}, "at-new", "personal", "login superseded"},
{"not handed, the same account, a lease older than it (H-A-140)", login, false, 9000, 0, func(p Paths, ask Ask, w WriteManaged) (string, error) {
return OnBindingChanged(p, []byte(`{"consumer":"laptop","licence":"personal","key":"personal","kind":"subscription","generation":9}`), ask, w)
}, "at-login", "personal", ""},
{"not handed, another account, a newer lease (H-A-140)", login, false, 200_000_000, now + 3*7_200_000, func(p Paths, ask Ask, w WriteManaged) (string, error) {
return OnBindingChanged(p, []byte(`{"consumer":"laptop","licence":"personal","key":"personal","kind":"subscription","generation":9}`), ask, w)
}, "at-login", "someone@example.org", ""},
{"a file holding the lease, older than the one handed", lease, false, 0, 0, func(p Paths, ask Ask, w WriteManaged) (string, error) {
return FirstBinding(p, ask, w)
}, "at-new"},
}, "at-new", "", ""},
} {
t.Run(c.name, func(t *testing.T) {
p, w := node(t, "laptop")
writeFile(t, p.binding(), `{"licence":"personal","key":"personal","kind":"subscription","generation":2}`+"\n")
writeFile(t, p.credentials(), c.file)
if c.account != "" {
writeFile(t, p.account(), `{"oauthAccount":{"accountUuid":"u-1","emailAddress":"`+c.account+`"}}`)
}
if c.handed {
manager, _ := GenerateKeyPair()
if a, err := GrantFor(p, manager.PublicKey); err != nil || a.Sealed == nil {
@@ -441,9 +455,13 @@ func TestALoginNotHandedIsNeverWrittenOverAndItsSuccessorReplacesIt(t *testing.T
return current(address, args)
}
before, _ := os.ReadFile(p.credentials())
if _, err := c.heard(p, ask, writer(w)); err != nil {
line, err := c.heard(p, ask, writer(w))
if err != nil {
t.Fatal(err)
}
if !strings.Contains(line, c.reason) {
t.Fatalf("the line %s lacks %q", line, c.reason)
}
got := creds(t, p)
if got["accessToken"] != c.want {
t.Fatalf("the file holds %v, want %s", got["accessToken"], c.want)
+15 -2
View File
@@ -633,9 +633,19 @@ func Apply(p Paths, c Current, sealed SealedBox, write WriteManaged) (map[string
// 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.
// The latest login wins (design 39 §6): a lease of the same account that succeeds the login (H-A-139) —
// the manager adopted a later login to that account from another node — replaces it, said as "login
// superseded"; a login to another account than the lease's licence is always kept (H-A-140, hq
// design/47-H1-answers-59 b75d1386).
superseded := false
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
id := ReadIdentity(p.account())
if id == nil || id.EmailAddress == "" || id.EmailAddress != c.Licence || !Succeeds(local, g) {
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
}
superseded = true
log.Printf("login superseded: a later login to the account of licence %s was adopted by the manager (generation %d)", c.Licence, c.Generation)
}
// 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, or the same lineage with a later access expiry, as the manager's refresh yields it): a lease older than the login is never
@@ -646,6 +656,9 @@ func Apply(p Paths, c Current, sealed SealedBox, write WriteManaged) (map[string
return map[string]any{"applied": false, "licence": c.Licence, "reason": LoginKept}, nil
}
d := DecideApply(GrantOf(local), g, switched || HoldsLogin(local))
if d.Apply && superseded {
out["reason"] = "login superseded"
}
if d.Apply {
next := WithGrant(local, g)
if switched {