From 121125aa502b679cd2f1bb7a0241d4038188810d Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 14:47:49 +0100 Subject: [PATCH] claude-code: the latest login wins over a login not handed to the manager MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A login not handed is kept only against a lease that does not succeed it; a lease of the same account that succeeds it replaces it, said as login superseded. A login to another account is kept until the manager adopts it. Builds: ADR 0206, ADR 0209, design 39 §6, H-A-138 (hq design/47-H1-answers-59 175ed320), H-A-139 (hq design/47-H1-answers-59 e359911e), H-A-140 (hq design/47-H1-answers-59 b75d1386) --- .../cmd/claude-code/claude_code_test.go | 40 ++++++++++++++----- modules/claude-code/cmd/claude-code/node.go | 17 +++++++- 2 files changed, 44 insertions(+), 13 deletions(-) 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 5a8f39e97..82f8630a4 100644 --- a/modules/claude-code/cmd/claude-code/claude_code_test.go +++ b/modules/claude-code/cmd/claude-code/claude_code_test.go @@ -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) diff --git a/modules/claude-code/cmd/claude-code/node.go b/modules/claude-code/cmd/claude-code/node.go index b5b4ed4af..ab876033f 100644 --- a/modules/claude-code/cmd/claude-code/node.go +++ b/modules/claude-code/cmd/claude-code/node.go @@ -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 { -- 2.54.0