claude-code: a login handed is replaced only by a grant of a newer lineage
One reason, login kept, on both paths, with a journal line; the not-handed return skips the binding's generation on purpose. Builds: ADR 0206, ADR 0209, design 39 §3 §6, design 14, H-A-138 (hq design/47-H1-answers-59 175ed320)
This commit is contained in:
@@ -370,22 +370,29 @@ func TestALoginNotHandedIsNeverWrittenOverAndItsSuccessorReplacesIt(t *testing.T
|
||||
for _, c := range []struct {
|
||||
name, file string
|
||||
handed bool
|
||||
offeredRTE int64 // the refresh token expiry the grant offered carries, 0 for none
|
||||
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, func(p Paths, ask Ask, w WriteManaged) (string, error) {
|
||||
{"the start's first view, a login made while stopped", login, false, 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, func(p Paths, ask Ask, w WriteManaged) (string, error) {
|
||||
{"binding.changed heard while it runs, before the grant was asked", login, false, 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, 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, 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", login, true, func(p Paths, ask Ask, w WriteManaged) (string, error) {
|
||||
{"the successor of a login handed, of a newer lineage", login, true, 20000, 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 file holding the lease, older than the one handed", lease, false, func(p Paths, ask Ask, w WriteManaged) (string, error) {
|
||||
{"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) {
|
||||
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) {
|
||||
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) {
|
||||
return FirstBinding(p, ask, w)
|
||||
}, "at-new"},
|
||||
} {
|
||||
@@ -405,6 +412,14 @@ 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 err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return json.Marshal(map[string]any{"name": args.(map[string]any)["name"], "generation": 1, "sealed": box})
|
||||
}
|
||||
return current(address, args)
|
||||
}
|
||||
before, _ := os.ReadFile(p.credentials())
|
||||
|
||||
@@ -122,6 +122,22 @@ func HoldsLogin(c Credentials) bool {
|
||||
return rt != ""
|
||||
}
|
||||
|
||||
// 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 {
|
||||
if offered.RefreshTokenExpiresAt == nil {
|
||||
return false
|
||||
}
|
||||
held := GrantOf(login)
|
||||
if held == nil || held.RefreshTokenExpiresAt == nil {
|
||||
return true
|
||||
}
|
||||
return *offered.RefreshTokenExpiresAt > *held.RefreshTokenExpiresAt
|
||||
}
|
||||
|
||||
// LoginNotHanded says the file holds a login that was never handed to the manager: handed is the fingerprint
|
||||
// of the refresh token claude_code_grant last sealed, "" when none was. Such a login is newer than anything the
|
||||
// manager holds, so no grant may overwrite it before the manager adopts it (ADR 0206, ADR 0209).
|
||||
|
||||
@@ -24,6 +24,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
@@ -630,11 +631,20 @@ func Apply(p Paths, c Current, sealed SealedBox, write WriteManaged) (map[string
|
||||
// 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)) {
|
||||
return map[string]any{"applied": false, "licence": c.Licence, "reason": "login-not-adopted"}, nil
|
||||
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
|
||||
}
|
||||
// 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.
|
||||
d := DecideApply(GrantOf(local), g, switched || HoldsLogin(local))
|
||||
if d.Apply {
|
||||
next := WithGrant(local, g)
|
||||
|
||||
Reference in New Issue
Block a user