diff --git a/modules/claude-code/cmd/claude-code/claude_code_test.go b/modules/claude-code/cmd/claude-code/claude_code_test.go index da3ebce75..5a8f39e97 100644 --- a/modules/claude-code/cmd/claude-code/claude_code_test.go +++ b/modules/claude-code/cmd/claude-code/claude_code_test.go @@ -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) } diff --git a/modules/claude-code/cmd/claude-code/grant.go b/modules/claude-code/cmd/claude-code/grant.go index c06e45336..7b28a383e 100644 --- a/modules/claude-code/cmd/claude-code/grant.go +++ b/modules/claude-code/cmd/claude-code/grant.go @@ -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 } diff --git a/modules/claude-code/cmd/claude-code/node.go b/modules/claude-code/cmd/claude-code/node.go index 45f5ecd5c..2615fcc76 100644 --- a/modules/claude-code/cmd/claude-code/node.go +++ b/modules/claude-code/cmd/claude-code/node.go @@ -635,11 +635,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))