Tameness

Tameness is a measure of how resistant your pet is to going wild (or feral). A wild pet becomes peaceful, a feral pet becomes hostile. Tameness is a value from 1 to 20, on a scale something like this:
 * 1 = nearly wild:  Your pet might go wild soon if you somehow become level-separated or abuse it before it eats.
 * 5 = tame:  Your pet can stand to be level-separated from you for a brief amount of time.
 * 10 = domesticated: Your pet can handle being level-separated from you for quite some time.
 * 15 = loyal: Your pet can stand to be level-separated for quite some time, and will even put up with accidental abuse.
 * 20 = fiercy loyal:  Your pet can be level-separated for a long time, and will even put up with occasional abuse.

You can #chat with most pets to get a general indication of how tame they are, although other factors such as hunger, confusion, fright, or being caught in a trap can make this difficult. In general, dogs will whine, cats will yowl, and horses will neigh if they are not content (tameness below 5, or possibly just hungry). Get them some food as soon as possible to see if that helps. A stethoscope can help you sort out some of the state conditions, but does not diagnose the level of tameness.

Initial tameness

 * Domestic animals start with a tameness of 10.
 * Dragons hatched from eggs start with a tameness of 5.
 * Other pets hatched from eggs start with a tameness of 20.
 * Pets that come from figurines or charmed with a scroll of taming, magic harp, or charm monster spell usually start with a tameness of 5.
 * Pets that come from the #monster extended command start with a tameness of 5.
 * Pets that come from a split (puddings, gremlins, etc.) start with a tameness of 5.
 * A tame djinni from a magic lamp or smoky potion starts with a tameness of 5.
 * Pets that are revived from the dead keep their tameness from before, if they come back tame. There's always a chance it will be revived hostile.
 * Past transgressions are remembered. Abused pets might be wild or feral.  If you killed the pet, it will be understandably mad about that.
 * Pets from a former adventurer's bones file will be feral. Re-tame them or kill them.
 * Your guardian angel on the Astral plane starts with a tameness of 10.

Increasing tameness

 * Tameness increases by +1 when a pet eats.
 * Carnivores and Omnivores are easy to keep tame because they will eat corpses.
 * Metallivores are easy to keep tame because they will eat metal junk.
 * Herbivores can be kept tame by carefully managing your comestibles.
 * Monsters that do not eat are difficult to keep tame. Do not abuse them or leave them on other dungeon levels.
 * Reading a blessed Book of the Dead will increase tameness of undead pets by +1 (and can tame nearby undead)

Decreasing tameness

 * Attacking your pet, displacing it in to a trap, or unsuccessfully untrapping it from a bear trap reduces tameness by -1
 * If you aggravate monsters or are causing conflict, tameness is cut in half instead.
 * Be very careful with arrows and other projectiles when your pet is directly behind a monster you are attacking.
 * -1 tameness for every 150 turns spent on a different level than the player.
 * A pet reduced to zero this way may go wild but stay peaceful, or go hostile.
 * A pet that would have starved to death in this situation will go hostile.
 * Throwing food at a former pet will anger it if it is not a domestic animal.
 * If you displace a trapped pet, there is a 1 in [tameness] that chance it will become hostile.
 * A pet that becomes unleashed by a trap (level teleport, trap door, hole, etc.) loses 1 point of tameness.
 * A pet choked by a cursed leash may lose 1 point of tameness. This will never reduce tameness below 1.
 * The chance of this -1 loss being waived is 1 in [tameness]. So the higher the current tameness, the more likely you lose 1 point.
 * Cursed leashes are very bad for keeping pets tame, but a pet will never turn on you solely because of the cursed leash.
 * Be extra careful around traps if your pet is on a cursed leash, as pushing your pet into a trap can put it over the edge.
 * Also watch out for stun or confusion states, if you accidentally hit your pet, it could be the last straw.
 * For the love of Marduk, I hope you don't have Stormbringer wielded if your leash is cursed.
 * -1 tameness every time you use #ride to try to mount a saddled pet, unless you are a knight. Tameness also influences your chance of success.
 * -1 tameness for kicking or whipping your mount (although this can make it move faster).
 * It can be dangerous to try to ride a pet with very low tameness, chat with it or feed it first if you can.
 * Be wary if your mount gets caught in a trap while you are riding. Pets and traps just do not mix well.

Strategy
In most cases, as long as you stay on the same level as your pet, and keep it from going hungry, your pet will stay loyal to you. Consider #praying if both you and your pet are hungry; pets don't really have that option. If you fall down a hole or step on a level teleport and leave your pet behind, try to go back for it as soon as you can. If your pet gets lost this way, it may be harder to figure out where the pet ended up. Telepathy might help you quickly scan the nearby floors. If you favor exotic pets, have a spare source of taming so you can recover from unfortunate separating circumstances. If you don't manage to find your old pet, you can always try to charm a new one. Laying eggs as a gargoyle or cockatrice can score you a very loyal pet, and laying eggs as a dragon gets you a pretty strong one.