contexts: clear current_context on delete; drop dead comment · Entire

contexts: clear current_context on delete; drop dead comment

e26a233main·

toothbrush·1mo ago·3 files·+14 added/-20 removed

RemoveCurrentContext relies on Delete's clear and drops its redundant reassignment.

Co-Authored-By: Claude Opus 4.8 (1M context) noreply@anthropic.com

Sessions

49c99a4a7077View transcript

[?
Debug Cluster Context Git Remote LookupClaude Code·Opus 4.8[1m]·1 step](/content/gh/entireio/cli/session/2946988e-fe9b-4ca5-8f30-3c60037a41d9#timeline-49c99a4a7077/index.html)

Changes

3

29 unmodified lines

30
31
32
33
34
33
34
35
36
37
5 unmodified lines

43
44
45
46
47
48
47
48
49
50
49
50
51

29 unmodified lines

// RemoveCurrentContext deletes the active context from contexts.json and
// its keyring token, advancing current_context to whatever remains. It is
// a no-op (returns nil) when there is no current context. Used by logout.
// its keyring token, clearing current_context. It is a no-op (returns nil)
// when there is no current context. Used by logout.
func RemoveCurrentContext() error {
    // Read-modify-write in a single locked Modify so the context we delete
    // is exactly the one we capture the keychain slot from (separate Load +
5 unmodified lines

return false, nil
    }
    svc, handle = current.KeychainService, current.Handle
    // Delete clears current_context because we're deleting the active
    // one — logged out means logged out, no switch to another identity.
    f.Delete(current.Name)
    // Delete advances current_context to a surviving context; for logout
    // that would silently re-authenticate as another account. Leave no
    // active context — logged out means logged out.
    f.CurrentContext = ""
    return true, nil
}); err != nil {
    return fmt.Errorf("remove current context: %w", err)
}

Mcmd/entire/cli/auth/context_store.go+4/-6

51 unmodified lines

52
53
54
55
56
57
58
59
60
55
56
57
108 unmodified lines

166
167
168
175
176
169
170
171
172
173
174
175
4 unmodified lines

180
181
182
187
188
189
183
184
185

51 unmodified lines

// Contexts is the list of stored credentials. Order is preserved on
    // disk so list output stays stable across saves.
    Contexts []*Context `json:"contexts,omitempty"`

// NOTE: a legacy "cluster_contexts" map used to live here, binding a
    // cluster host to one context name. It's gone — cluster cores are cached
    // in discovery.ClusterCoresCache and the account is chosen fresh per
    // operation. json.Unmarshal ignores the obsolete key, so an old file
    // loads cleanly; the key drops out on the next write.
}

// FilePath returns $configDir/contexts.json after ensuring the directory
108 unmodified lines

}

// Delete drops the context with the given name. If the deleted context
// was current, the current pointer advances to whatever remains (or empty).
// Delete drops the context with the given name. If it was the current
// context, current_context is cleared — never reassigned to another
// context, so deleting your active login never silently switches you to a
// different identity.
func (f *File) Delete(name string) {
    if f == nil || name == "" {
        return
    }
    if f.CurrentContext == name {
        f.CurrentContext = ""
        if len(f.Contexts) > 0 {
            f.CurrentContext = f.Contexts[0].Name
        }
    }
}

Minternal/entireclient/contexts/contexts.go+4/-11

139 unmodified lines

140
141
142
143
143
144
145
146
2 unmodified lines

149
150
151
152
153
152
153
154
155
156
157
158
159

139 unmodified lines

}
}

func TestDelete_OfCurrentAdvancesPointer(t *testing.T) {
func TestDelete_OfCurrentClearsCurrent(t *testing.T) {
f := &contexts.File{
    CurrentContext: "first",
    Contexts: []*contexts.Context{
2 unmodified lines
    },
}
f.Delete("first")
if f.CurrentContext != "second" {
                
t.Errorf("after deleting current, CurrentContext = %q, want next remaining (%q)", f.CurrentContext, "second")
if f.CurrentContext != "" {
                
t.Errorf("after deleting current, CurrentContext = %q, want empty (no fallback to another identity)", f.CurrentContext)
}
if f.Find("second") == nil {
                
t.Error("Delete dropped the unrelated remaining context")
}
}

Minternal/entireclient/contexts/contexts_test.go+6/-3