From ca4aac2cd0fe3ab2a8fb625f45c62d523e31a23d Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Tue, 17 Nov 2026 07:02:14 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Keep=20Copilot=20config.json=20l?= =?UTF-8?q?inked=20to=20the=20shared=20login=20after=20the=20CLI's=20own?= =?UTF-8?q?=20save?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Copilot CLI's atomic save replaces the per-agent config.json symlink seconds after launch, so a later dashboard login never reached running agents. The pane poller now relinks within one poll interval, merging CLI-written non-credential fields into the shared file first (shared login wins), serialized against the hive's token writers. The #18445 guard for a private copy holding the only login is kept. Fixes #18671 Signed-off-by: Andy Anderson --- ...1-copilot-config-link-survives-cli-save.md | 1 + src/crux/agent/copilot_config_bridge.go | 153 ++++++++++++- .../agent/copilot_config_bridge_sync_test.go | 215 ++++++++++++++++++ src/crux/agent/copilot_token_store.go | 17 ++ src/crux/agent/copilottoken/copilottoken.go | 7 + src/crux/agent/manager_poll.go | 6 + 6 files changed, 387 insertions(+), 12 deletions(-) create mode 100644 changelog.d/fixed-18671-copilot-config-link-survives-cli-save.md create mode 100644 src/crux/agent/copilot_config_bridge_sync_test.go diff --git a/changelog.d/fixed-18671-copilot-config-link-survives-cli-save.md b/changelog.d/fixed-18671-copilot-config-link-survives-cli-save.md new file mode 100644 index 0000000000..3887e39435 --- /dev/null +++ b/changelog.d/fixed-18671-copilot-config-link-survives-cli-save.md @@ -0,0 +1 @@ +- **A dashboard Copilot re-login now reaches running Copilot agents without a restart** ([#18671](https://github.com/hivecommons/hive/issues/18671)). The Copilot CLI's own save, seconds after launch, replaced the freshly relinked `~/.copilot/config.json` symlink with a private copy again, so a later login written to the shared file only reached each agent after its next restart. The pane poller now checks the link on every tick and relinks it within one poll interval of a CLI save, first merging the fields the CLI wrote into the shared file so nothing it saved is lost; the shared login always wins over the token in the private copy, and a private copy holding the only login is still left in place. diff --git a/src/crux/agent/copilot_config_bridge.go b/src/crux/agent/copilot_config_bridge.go index eab63c8ecc..e1a4d1405d 100644 --- a/src/crux/agent/copilot_config_bridge.go +++ b/src/crux/agent/copilot_config_bridge.go @@ -1,15 +1,19 @@ package agent import ( + "context" "errors" "fmt" "io/fs" + "log/slog" "os" "path/filepath" + "reflect" "strings" "sync" "time" + "github.com/hivecommons/hive/crux/agent/copilottoken" "github.com/hivecommons/hive/crux/sharedfile" ) @@ -37,13 +41,37 @@ import ( // none, the private copy may be the only login the fleet has, so it is left in // place (logged once). A later dashboard login or token sync fills the shared // file and the next launch relinks. +// +// Keeping the link alive while the agent runs (#18671): the CLI does its own +// atomic save a few seconds after it starts, so a launch-time relink alone is +// undone almost at once and a later dashboard login never reaches the running +// agent. The pane poller therefore checks the link on every tick +// (relinkCopilotConfigAfterCLISave, one Lstat while the link is intact) and +// repairs it within one poll interval of the save. Before the private copy is +// moved aside, the fields the CLI wrote to it are merged into the shared file +// (mergeCopilotConfigIntoShared), so relinking loses nothing the CLI saved. +// The credential fields are the one exception: the shared file's login always +// wins, because the private copy holds whatever token the CLI had in memory, +// which is older than a dashboard login made since. // Repair triggers, recorded in the log. const ( copilotBridgeTriggerLaunch = "launch" copilotBridgeTriggerLoginPrompt = "login-prompt" + copilotBridgeTriggerCLISave = "cli-save" ) +// copilotConfigCredentialKeys are the config.json fields that together make up +// the login. The merge never copies them from a private copy into the shared +// file: the shared login is authoritative (dashboard device flow, token sync), +// and mixing one file's token map with another's identity yields a config the +// CLI reads as logged out. +var copilotConfigCredentialKeys = map[string]bool{ + "copilotTokens": true, + "lastLoggedInUser": true, + "loggedInUsers": true, +} + // copilotConfigBackupInfix names a private config moved aside by the repair. const copilotConfigBackupInfix = ".bak-" @@ -142,34 +170,105 @@ func copilotConfigDiverged(link, target string) bool { // with a nil error when there is nothing to repair (a symlink, an absent path, // a directory). func repairCopilotConfigBridge(link, target string) (string, error) { + backup, _, err := repairCopilotConfigBridgeMerged(link, target) + return backup, err +} + +// repairCopilotConfigBridgeMerged is repairCopilotConfigBridge that also +// reports whether CLI-written fields were merged into the shared file first. +// When another hive writer holds the shared config it does nothing this time +// ("" and a nil error): the poller retries on its next tick. +func repairCopilotConfigBridgeMerged(link, target string) (string, bool, error) { copilotBridgeMu.Lock() defer copilotBridgeMu.Unlock() info, err := os.Lstat(link) if err != nil || !info.Mode().IsRegular() { - return "", nil + return "", false, nil } same, err := claudeBridgeLinkIsTarget(link, target) if err != nil { - return "", nil + return "", false, nil } if same { - return "", fmt.Errorf("%w: %s", errCopilotBridgeLinkIsTarget, link) + return "", false, fmt.Errorf("%w: %s", errCopilotBridgeLinkIsTarget, link) } if copilotCredentialFileHasTokens(link) && !copilotCredentialFileHasTokens(target) { - return "", errCopilotBridgeSharedEmpty + return "", false, errCopilotBridgeSharedEmpty + } + // Best effort: a private copy or shared file that cannot be read or parsed + // is relinked without a merge, exactly as before #18671; the backup still + // holds every field the CLI wrote. + merged, busy, _ := mergeCopilotConfigIntoShared(link, target) + if busy { + return "", false, nil } backup, err := moveAsideCopilotConfig(link, copilotBridgeNow()) if err != nil { - return "", fmt.Errorf("move private copilot config aside: %w", err) + return "", merged, fmt.Errorf("move private copilot config aside: %w", err) } pruneCopilotConfigBackups(link, backup) if err := os.Symlink(target, link); err != nil { - // EEXIST: the CLI saved again between the rename and now. The next - // launch repairs that one. - return backup, fmt.Errorf("restore copilot config symlink: %w", err) + // EEXIST: the CLI saved again between the rename and now. The poller + // repairs that one on its next tick. + return backup, merged, fmt.Errorf("restore copilot config symlink: %w", err) + } + return backup, merged, nil +} + +// mergeCopilotConfigIntoShared folds the fields the Copilot CLI wrote to the +// private copy at link into the shared config at target, so relinking does not +// throw them away. Credential fields (copilotConfigCredentialKeys) are never +// copied: the shared login wins. The shared file is rewritten only when the +// merge changes it, through copilottoken.WriteConfig (temp file + rename, the +// shared mode, owner preserved), and only while holding sharedCopilotConfigMu +// so it cannot race a dashboard login. busy reports that another hive writer +// holds that lock; nothing was read or written. Contents are never logged. +func mergeCopilotConfigIntoShared(link, target string) (merged, busy bool, err error) { + privateData, err := sharedfile.ReadNoFollow(link, copilotConfigMaxBytes) + if err != nil { + return false, false, err } - return backup, nil + private, err := copilottoken.ParseConfig(privateData) + if err != nil { + return false, false, err + } + if !sharedCopilotConfigMu.TryLock() { + return false, true, nil + } + defer sharedCopilotConfigMu.Unlock() + sharedData, err := sharedfile.ReadNoFollow(target, copilotConfigMaxBytes) + if err != nil { + return false, false, err + } + shared, err := copilottoken.ParseConfig(sharedData) + if err != nil { + return false, false, err + } + out := mergeCopilotConfigFields(shared, private) + if reflect.DeepEqual(out, shared) { + return false, false, nil + } + if err := copilottoken.WriteConfig(target, out, sharedConfigDesiredMode); err != nil { + return false, false, err + } + return true, false, nil +} + +// mergeCopilotConfigFields returns shared overlaid with every non-credential +// field of private. Neither input is modified. +func mergeCopilotConfigFields(shared, private map[string]interface{}) map[string]interface{} { + out := make(map[string]interface{}, len(shared)+len(private)) + for k, v := range shared { + out[k] = v + } + for k, v := range private { + if copilotConfigCredentialKeys[k] { + continue + } + out[k] = v + } + return out } // moveAsideCopilotConfig renames path to a free .bak-[-N] @@ -215,7 +314,7 @@ func pruneCopilotConfigBackups(link, keep string) { // rebridgeCopilotConfig runs the repair for one agent and logs the outcome. It // reports whether the link was restored. func (m *Manager) rebridgeCopilotConfig(agentName, link, target, trigger string) bool { - backup, err := repairCopilotConfigBridge(link, target) + backup, merged, err := repairCopilotConfigBridgeMerged(link, target) if err != nil { if prev, loaded := copilotBridgeWarned.Swap(link, err.Error()); loaded && prev == err.Error() { return false @@ -229,16 +328,46 @@ func (m *Manager) rebridgeCopilotConfig(agentName, link, target, trigger string) return false } // A completed repair is routine self-healing, not a fault: WARN made the - // fleet self-report it as an unclassified repeated error (#18497). - m.logger.Info("copilot config bridge: a private config.json had replaced the shared-login symlink; re-linked", + // fleet self-report it as an unclassified repeated error (#18497). The + // running-agent repair follows every CLI save, so it logs at DEBUG; the + // launch and login-prompt repairs stay at INFO. + level := slog.LevelInfo + if trigger == copilotBridgeTriggerCLISave { + level = slog.LevelDebug + } + m.logger.Log(context.Background(), level, "copilot config bridge: a private config.json had replaced the shared-login symlink; re-linked", "agent", agentName, "trigger", trigger, "backup", backup, + "merged_cli_fields", merged, "cause", "Copilot CLI saves its config with a temp file renamed over the link", "see", "hivecommons/hive#18443") return true } +// relinkCopilotConfigAfterCLISave is the pane poller's per-tick hook (#18671): +// when the Copilot CLI's own atomic save has replaced a running agent's +// config.json symlink with a private regular file, merge the CLI's fields into +// the shared file and relink, so a later dashboard login reaches the agent +// without a restart. An intact link costs one Lstat. The #18445 guard holds: a +// private copy holding the only login is left in place. +func (m *Manager) relinkCopilotConfigAfterCLISave(agent *AgentProcess) bool { + m.mu.RLock() + name, uid, backend := agent.Name, agent.UID, effectiveBackend(agent) + m.mu.RUnlock() + if backend != "copilot" { + return false + } + link, target, ok := copilotConfigBridgePaths(name, uid, backend) + if !ok { + return false + } + if info, err := os.Lstat(link); err != nil || !info.Mode().IsRegular() { + return false + } + return m.rebridgeCopilotConfig(name, link, target, copilotBridgeTriggerCLISave) +} + // repairCopilotConfigOnLoginPrompt is the poller's hook: a Copilot agent stuck // at a login prompt whose config.json diverged from the shared login is // relinked, so the credential probe then evaluates the shared login and the diff --git a/src/crux/agent/copilot_config_bridge_sync_test.go b/src/crux/agent/copilot_config_bridge_sync_test.go new file mode 100644 index 0000000000..98cfa28314 --- /dev/null +++ b/src/crux/agent/copilot_config_bridge_sync_test.go @@ -0,0 +1,215 @@ +package agent + +import ( + "os" + "strings" + "testing" + + "github.com/hivecommons/hive/crux/agent/copilottoken" + "github.com/hivecommons/hive/crux/config" +) + +// Running-agent relink after the Copilot CLI's own save (#18671). Every token +// value carries copilotBridgeTestSentinel so assertNoSentinelLogged proves no +// log line leaks token material. + +const ( + // copilotBridgeTestCLISaved is what the CLI's save leaves behind: the token + // it had in memory plus fields it wrote itself. + copilotBridgeTestCLISaved = `{"copilotTokens":{"https://github.com:fake-user":"gho_older_` + copilotBridgeTestSentinel + `"},"banner":"never","trustedFolders":["/work"]}` + // copilotBridgeTestNewToken is the token a later dashboard login writes. + copilotBridgeTestNewToken = "gho_dashboard_" + copilotBridgeTestSentinel +) + +func readCopilotBridgeConfig(t *testing.T, path string) map[string]interface{} { + t.Helper() + cfg, err := copilottoken.ReadConfig(path) + if err != nil { + t.Fatalf("read %s: %v", path, err) + } + return cfg +} + +func copilotBridgeTokenAt(t *testing.T, path string) string { + t.Helper() + tokens, ok := readCopilotBridgeConfig(t, path)["copilotTokens"].(map[string]interface{}) + if !ok { + t.Fatalf("%s has no copilotTokens map", path) + } + tok, _ := tokens["https://github.com:fake-user"].(string) + return tok +} + +// simulateDashboardLogin writes a new login to the shared file the way the +// dashboard device flow does: a whole-file atomic rewrite of the shared path. +func simulateDashboardLogin(t *testing.T, target, token string) { + t.Helper() + cfg := readCopilotBridgeConfig(t, target) + cfg["copilotTokens"] = map[string]interface{}{"https://github.com:fake-user": token} + if err := copilottoken.WriteConfig(target, cfg, sharedConfigDesiredMode); err != nil { + t.Fatal(err) + } +} + +func TestRelinkCopilotConfigAfterCLISave_RelinksAndMergesCLIFields(t *testing.T) { + m, buf, ap, link, target := copilotBridgeFixture(t) + simulateCopilotSave(t, link, copilotBridgeTestCLISaved) + + // One poll tick. + if !m.relinkCopilotConfigAfterCLISave(ap) { + t.Fatal("a private copy left by the CLI's save must be relinked on the next poll") + } + assertCopilotLinked(t, link, target) + + cfg := readCopilotBridgeConfig(t, target) + if cfg["banner"] != "never" { + t.Fatalf("CLI-written field lost by the relink: banner=%v", cfg["banner"]) + } + if folders, ok := cfg["trustedFolders"].([]interface{}); !ok || len(folders) != 1 || folders[0] != "/work" { + t.Fatalf("CLI-written field lost by the relink: trustedFolders=%v", cfg["trustedFolders"]) + } + // The shared login wins over the token the CLI had in memory. + if got := copilotBridgeTokenAt(t, target); got != "gho_"+copilotBridgeTestSentinel { + t.Fatal("the merge must keep the shared login, not the private copy's older token") + } + if b := copilotBackups(t, link); len(b) != 1 { + t.Fatalf("want one backup of the private copy, got %v", b) + } + + // The next tick, with the link intact, does nothing. + if m.relinkCopilotConfigAfterCLISave(ap) { + t.Fatal("an intact link must not be repaired again") + } + assertNoSentinelLogged(t, buf) +} + +func TestRelinkCopilotConfigAfterCLISave_DashboardLoginReachesRunningAgent(t *testing.T) { + m, buf, ap, link, target := copilotBridgeFixture(t) + + // The CLI's save seconds after launch, then the poller's relink. + simulateCopilotSave(t, link, copilotBridgeTestLoggedIn) + if !m.relinkCopilotConfigAfterCLISave(ap) { + t.Fatal("private copy must be relinked") + } + + // A dashboard re-login updates only the shared file. The running agent's + // effective config (the file at its own path) sees it with no restart. + simulateDashboardLogin(t, target, copilotBridgeTestNewToken) + if got := copilotBridgeTokenAt(t, link); got != copilotBridgeTestNewToken { + t.Fatal("running agent's config.json must see the dashboard login without a restart") + } + + // The CLI saves again with the token it still holds in memory. The next + // tick relinks without rolling the fleet back to that older token. + simulateCopilotSave(t, link, copilotBridgeTestCLISaved) + if !m.relinkCopilotConfigAfterCLISave(ap) { + t.Fatal("second CLI save must be relinked too") + } + assertCopilotLinked(t, link, target) + if got := copilotBridgeTokenAt(t, link); got != copilotBridgeTestNewToken { + t.Fatal("a later CLI save must not overwrite the dashboard login") + } + if got := copilotBridgeTokenAt(t, target); got != copilotBridgeTestNewToken { + t.Fatal("shared login must still be the dashboard login") + } + if b := copilotBackups(t, link); len(b) != 1 { + t.Fatalf("only the newest backup is kept, got %v", b) + } + assertNoSentinelLogged(t, buf) +} + +func TestRelinkCopilotConfigAfterCLISave_KeepsOnlyLoginWhenSharedEmpty(t *testing.T) { + m, buf, ap, link, target := copilotBridgeFixture(t) + simulateCopilotSave(t, link, copilotBridgeTestLoggedIn) + writeCopilotBridgeFile(t, target, copilotBridgeTestNoLogin) + + for i := 0; i < 3; i++ { + if m.relinkCopilotConfigAfterCLISave(ap) { + t.Fatal("a private copy holding the only login must not be relinked") + } + } + if info, err := os.Lstat(link); err != nil || !info.Mode().IsRegular() { + t.Fatalf("private login must stay in place: info=%v err=%v", info, err) + } + if data, err := os.ReadFile(target); err != nil || string(data) != copilotBridgeTestNoLogin { + t.Fatalf("shared file must not be touched by a refused repair: err=%v", err) + } + if got := strings.Count(buf.String(), "repair not applied"); got != 1 { + t.Fatalf("refusal must be logged once across polls, got %d:\n%s", got, buf.String()) + } + assertNoSentinelLogged(t, buf) +} + +func TestRelinkCopilotConfigAfterCLISave_WaitsForSharedWriter(t *testing.T) { + m, _, ap, link, target := copilotBridgeFixture(t) + simulateCopilotSave(t, link, copilotBridgeTestCLISaved) + + sharedCopilotConfigMu.Lock() + relinked := m.relinkCopilotConfigAfterCLISave(ap) + sharedCopilotConfigMu.Unlock() + if relinked { + t.Fatal("must not relink while another writer holds the shared config") + } + if info, err := os.Lstat(link); err != nil || !info.Mode().IsRegular() { + t.Fatalf("private copy must stay until the next tick: info=%v err=%v", info, err) + } + if b := copilotBackups(t, link); len(b) != 0 { + t.Fatalf("nothing may be moved aside while busy, got %v", b) + } + + if !m.relinkCopilotConfigAfterCLISave(ap) { + t.Fatal("next tick must relink once the writer is done") + } + assertCopilotLinked(t, link, target) + if readCopilotBridgeConfig(t, target)["banner"] != "never" { + t.Fatal("CLI-written field must be merged on the retry") + } +} + +func TestRelinkCopilotConfigAfterCLISave_IgnoresOtherAgents(t *testing.T) { + m, _, _, link, _ := copilotBridgeFixture(t) + simulateCopilotSave(t, link, copilotBridgeTestNoLogin) + other := &AgentProcess{Name: copilotBridgeTestAgentName, UID: copilotBridgeTestUID, Config: config.AgentConfig{Backend: "claude"}} + if m.relinkCopilotConfigAfterCLISave(other) { + t.Fatal("non-copilot backends are not relinked by the copilot hook") + } + noHome := &AgentProcess{Name: "planner", Config: config.AgentConfig{Backend: "copilot"}} + if m.relinkCopilotConfigAfterCLISave(noHome) { + t.Fatal("agent without a per-agent home has nothing to relink") + } + if info, err := os.Lstat(link); err != nil || !info.Mode().IsRegular() { + t.Fatalf("file must stay in place: info=%v err=%v", info, err) + } +} + +func TestMergeCopilotConfigFields(t *testing.T) { + shared := map[string]interface{}{ + "copilotTokens": map[string]interface{}{"k": "shared"}, + "lastLoggedInUser": "https://github.com:shared", + "model": "shared-model", + "sharedOnly": true, + } + private := map[string]interface{}{ + "copilotTokens": map[string]interface{}{"k": "private"}, + "loggedInUsers": []interface{}{"https://github.com:private"}, + "model": "cli-model", + "privateOnly": "x", + } + out := mergeCopilotConfigFields(shared, private) + + if tok := out["copilotTokens"].(map[string]interface{})["k"]; tok != "shared" { + t.Fatalf("copilotTokens must come from shared, got %v", tok) + } + if out["lastLoggedInUser"] != "https://github.com:shared" { + t.Fatalf("identity must come from shared, got %v", out["lastLoggedInUser"]) + } + if _, ok := out["loggedInUsers"]; ok { + t.Fatal("a credential field absent from shared must not be imported from private") + } + if out["model"] != "cli-model" || out["privateOnly"] != "x" || out["sharedOnly"] != true { + t.Fatalf("non-credential fields: want private over shared, got %v", out) + } + if shared["model"] != "shared-model" || len(shared) != 4 { + t.Fatal("inputs must not be modified") + } +} diff --git a/src/crux/agent/copilot_token_store.go b/src/crux/agent/copilot_token_store.go index e5384909e7..7893bb811a 100644 --- a/src/crux/agent/copilot_token_store.go +++ b/src/crux/agent/copilot_token_store.go @@ -1,6 +1,8 @@ package agent import ( + "sync" + "github.com/hivecommons/hive/crux" "github.com/hivecommons/hive/crux/agent/copilottoken" ) @@ -28,15 +30,30 @@ func readCopilotConfig(path string) (map[string]interface{}, error) { return copilottoken.ReadConfig(path) } +// sharedCopilotConfigMu serializes the hive's own writes to the shared +// Copilot config.json: the token writers below (dashboard login, token sync, +// expired-token clear) and the config bridge's merge of CLI-written fields +// (mergeCopilotConfigIntoShared, #18671). Without it a merge that read the +// shared file just before a dashboard login could rename its stale copy over +// the new login. The bridge only ever TryLocks it, so a token writer blocked +// on a GitHub login lookup never stalls an agent's pane poller. +var sharedCopilotConfigMu sync.Mutex + func clearExpiredTokens() error { + sharedCopilotConfigMu.Lock() + defer sharedCopilotConfigMu.Unlock() return copilottoken.ClearExpiredTokens(sharedCopilotConfigPath, sharedConfigDesiredMode) } func restoreCopilotTokens(path, token string) error { + sharedCopilotConfigMu.Lock() + defer sharedCopilotConfigMu.Unlock() return copilottoken.RestoreTokens(path, token, sharedConfigDesiredMode, githubTokenLogin) } func replaceCopilotTokens(path, token string) error { + sharedCopilotConfigMu.Lock() + defer sharedCopilotConfigMu.Unlock() return copilottoken.ReplaceTokens(path, token, sharedConfigDesiredMode, githubTokenLogin) } diff --git a/src/crux/agent/copilottoken/copilottoken.go b/src/crux/agent/copilottoken/copilottoken.go index e9d97f78a6..8a696b922c 100644 --- a/src/crux/agent/copilottoken/copilottoken.go +++ b/src/crux/agent/copilottoken/copilottoken.go @@ -119,6 +119,13 @@ func ReadConfig(path string) (map[string]interface{}, error) { if err != nil { return nil, err } + return ParseConfig(data) +} + +// ParseConfig is ReadConfig over bytes the caller already read (for example +// with sharedfile.ReadNoFollow): it strips the CLI's // comment lines and +// unmarshals the remainder, returning an empty map for a JSON null. +func ParseConfig(data []byte) (map[string]interface{}, error) { var cleaned []byte for _, line := range strings.Split(string(data), "\n") { if strings.HasPrefix(strings.TrimSpace(line), "//") { diff --git a/src/crux/agent/manager_poll.go b/src/crux/agent/manager_poll.go index f14a0dcae3..d8aabec3f1 100644 --- a/src/crux/agent/manager_poll.go +++ b/src/crux/agent/manager_poll.go @@ -27,6 +27,12 @@ func (m *Manager) pollTmuxOutputForAgent(agent *AgentProcess, ctx context.Contex case <-ctx.Done(): return case <-ticker.C: + // The Copilot CLI's own atomic save replaces the config.json + // symlink seconds after launch; relink it within one poll interval + // so a later dashboard login reaches this running agent (#18671). + // One Lstat while the link is intact; a no-op for other backends. + m.relinkCopilotConfigAfterCLISave(agent) + // Scrollback capture for dashboard display + ring buffer diff output := m.captureTmuxPaneForAgent(agent) if output == "" {