claude-code: a login is succeeded by its own refresh, not only by a newer lineage

The manager's refresh keeps the licence's refresh-token expiry, so a grant of the
same lineage with a later access expiry replaces the login; a grant with no
mark, an older lineage or no later access expiry is the older lease and is kept.

Builds: ADR 0206, ADR 0209, design 39 §3 §6, design 14, H-A-138 (hq design/47-H1-answers-59 175ed320), H-A-139 (hq design/47-H1-answers-59 e359911e)
This commit is contained in:
2026-11-20 14:25:47 +01:00
parent a8a0cfeb8f
commit 0934292f47
3 changed files with 41 additions and 17 deletions
@@ -371,28 +371,38 @@ func TestALoginNotHandedIsNeverWrittenOverAndItsSuccessorReplacesIt(t *testing.T
name, file string
handed bool
offeredRTE int64 // the refresh token expiry the grant offered carries, 0 for none
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
}{
{"the start's first view, a login made while stopped", login, false, 0, func(p Paths, ask Ask, w WriteManaged) (string, error) {
{"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"},
{"binding.changed heard while it runs, before the grant was asked", login, false, 0, func(p Paths, ask Ask, w WriteManaged) (string, error) {
{"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"},
{"the vault's change heard while it runs, before the grant was asked", login, false, 0, func(p Paths, ask Ask, w WriteManaged) (string, error) {
{"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"},
{"the successor of a login handed, of a newer lineage", login, true, 20000, func(p Paths, ask Ask, w WriteManaged) (string, error) {
{"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"},
{"a login handed, met by a lease older than it (H-A-138)", login, true, 100, func(p Paths, ask Ask, w WriteManaged) (string, error) {
{"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"},
{"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"},
{"a login handed, met by a lease with no lineage mark (H-A-138)", login, true, 0, func(p Paths, ask Ask, w WriteManaged) (string, error) {
{"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"},
{"a file holding the lease, older than the one handed", lease, false, 0, func(p Paths, ask Ask, w WriteManaged) (string, error) {
{"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"},
{"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"},
{"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"},
} {
@@ -412,9 +422,17 @@ func TestALoginNotHandedIsNeverWrittenOverAndItsSuccessorReplacesIt(t *testing.T
if address == SeatVerb("bindings") {
return json.RawMessage(`[{"consumer":"laptop","licence":"personal","key":"personal","kind":"subscription","generation":9}]`), nil
}
if address == VaultGet && c.offeredRTE != 0 {
g, _ := json.Marshal(Grant{AccessToken: "at-new", ExpiresAt: now + 3_600_000, RefreshTokenExpiresAt: &c.offeredRTE})
box, err := Seal(string(g), args.(map[string]any)["public_key"].(string))
if address == VaultGet && (c.offeredRTE != 0 || c.offeredExp != 0) {
exp := c.offeredExp
if exp == 0 {
exp = now + 3_600_000
}
g := Grant{AccessToken: "at-new", ExpiresAt: exp}
if c.offeredRTE != 0 {
g.RefreshTokenExpiresAt = &c.offeredRTE
}
gj, _ := json.Marshal(g)
box, err := Seal(string(gj), args.(map[string]any)["public_key"].(string))
if err != nil {
t.Fatal(err)
}
+9 -3
View File
@@ -125,9 +125,12 @@ func HoldsLogin(c Credentials) bool {
// LoginKept is the reason a grant is not written over a login (H-A-138).
const LoginKept = "login kept"
// NewerLineage says the offered grant's refresh token expires later than the login's: the manager marks a
// successor of the login so, and a lease older than the login never does.
func NewerLineage(login Credentials, offered Grant) bool {
// Succeeds says the offered grant replaces the login the file holds (H-A-138, H-A-139): its lineage is newer
// (its refresh token's expiry later than the login's, beyond one generation), or it is of the same lineage
// (sameGeneration) and its access token expires later than the login's, which only the manager's refresh of
// that login yields. No lineage mark, an older lineage, or the same lineage with no later access expiry is the
// older lease.
func Succeeds(login Credentials, offered Grant) bool {
if offered.RefreshTokenExpiresAt == nil {
return false
}
@@ -135,6 +138,9 @@ func NewerLineage(login Credentials, offered Grant) bool {
if held == nil || held.RefreshTokenExpiresAt == nil {
return true
}
if sameGeneration(held.RefreshTokenExpiresAt, offered.RefreshTokenExpiresAt) {
return offered.ExpiresAt > held.ExpiresAt
}
return *offered.RefreshTokenExpiresAt > *held.RefreshTokenExpiresAt
}
+4 -4
View File
@@ -638,11 +638,11 @@ func Apply(p Paths, c Current, sealed SealedBox, write WriteManaged) (map[string
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
// 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
// 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)
// (H-A-138, hq design/47-H1-answers-59 175ed320; H-A-139, e359911e).
if HoldsLogin(local) && !Succeeds(local, g) {
log.Printf("login kept: the grant offered for licence %s does not succeed 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))