From d574791bcf270cbcff6b05b21a7dd7f7470185d5 Mon Sep 17 00:00:00 2001 From: sama Pyb Date: Wed, 2 Dec 2026 23:15:17 +0800 Subject: [PATCH 1/2] fix(phone): apply --home early for codeaf phone serve (#3020) --- cmd/codeaf/app_home.go | 38 +++++++++++++++++++----------- cmd/codeaf/app_home_login_test.go | 39 +++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 13 deletions(-) diff --git a/cmd/codeaf/app_home.go b/cmd/codeaf/app_home.go index dfbf79f981..371cfffb44 100644 --- a/cmd/codeaf/app_home.go +++ b/cmd/codeaf/app_home.go @@ -45,23 +45,35 @@ func newAppFlags() appFlags { } } -// useAppHomeEarly applies `codeaf app --home ` at the very start of the -// process, before anything reads the state root. +// useAppHomeEarly applies `codeaf app --home ` and +// `codeaf phone serve --home ` at the very start of the process, before +// anything reads the state root. // -// main reads the state root before runApp has parsed a flag: the legacy-folder -// adoption, the first look at the root's identity (home.Witness), and the -// crash-telemetry consent, which is read from the profile under it. Left to -// runApp, an app started with --home would have its consent read from the -// command line's folder and could adopt the command line's legacy folder. -// Whatever is wrong with the rest of the command line is runApp's to say: this -// reads the flag the way runApp will and acts on nothing else. +// main reads the state root before runApp or runPhoneContext has parsed a flag: +// the legacy-folder adoption, the first look at the root's identity +// (home.Witness), and the crash-telemetry consent, which is read from the +// profile under it. Left to the command handler, a run started with --home +// would have its consent read from the default folder and could adopt the +// default folder's legacy folder. Whatever is wrong with the rest of the command +// line is the command handler's to say: this reads the flag the way the command +// will and acts on nothing else. func useAppHomeEarly(args []string) { - if len(args) == 0 || args[0] != "app" { + if len(args) == 0 { return } - names := newAppFlags() - _ = names.set.Parse(args[1:]) - _, _ = useAppHome(*names.home) + switch args[0] { + case "app": + names := newAppFlags() + _ = names.set.Parse(args[1:]) + _, _ = useAppHome(*names.home) + case "phone": + if len(args) < 2 || args[1] != "serve" { + return + } + names := newPhoneServeFlags() + _ = names.set.Parse(reorder(names.set, args[2:])) + _, _ = useAppHome(*names.home) + } } // useAppHome points this process at the state folder the app asked for with diff --git a/cmd/codeaf/app_home_login_test.go b/cmd/codeaf/app_home_login_test.go index c5ac092c8d..5465a16d02 100644 --- a/cmd/codeaf/app_home_login_test.go +++ b/cmd/codeaf/app_home_login_test.go @@ -76,6 +76,10 @@ func TestAppHomeFlagIsReadBeforeTheStateRootIsRead(t *testing.T) { {"app", "--home=" + state}, {"app", "-home", state}, {"app", "--turn-long", "5s", "--home", state}, + {"phone", "serve", "--home", state}, + {"phone", "serve", "--home=" + state}, + {"phone", "serve", "-home", state}, + {"phone", "serve", "--accept-older-list", "--home", state}, } { t.Setenv(home.EnvVar, "") t.Setenv(home.LoginEnvVar, "") @@ -89,12 +93,47 @@ func TestAppHomeFlagIsReadBeforeTheStateRootIsRead(t *testing.T) { } } +func TestPhoneServeHomeFlagAppliesBeforeTelemetryChoiceAndDefaultHome(t *testing.T) { + login := t.TempDir() + state := filepath.Join(t.TempDir(), "phone-state") + defaultProfile := filepath.Join(login, ".codeaf") + if err := os.MkdirAll(defaultProfile, 0o700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(defaultProfile, "config.yaml"), []byte("telemetry: false\n"), 0o600); err != nil { + t.Fatal(err) + } + if err := os.MkdirAll(state, 0o700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(state, "config.yaml"), []byte("telemetry: true\n"), 0o600); err != nil { + t.Fatal(err) + } + t.Setenv("HOME", login) + t.Setenv(home.EnvVar, "") + t.Setenv(home.LoginEnvVar, "") + t.Setenv("DO_NOT_TRACK", "") + t.Setenv("CODEAF_TELEMETRY", "") + + useAppHomeEarly([]string{"phone", "serve", "--home", state}) + if got := home.Dir(); got != state { + t.Fatalf("home.Dir() = %q, want %q", got, state) + } + if telemetryConfiguredOff() { + t.Fatalf("telemetryConfiguredOff() read default home (%s) instead of --home (%s)", defaultProfile, state) + } +} + func TestAppHomeFlagIsLeftAloneByEveryOtherCommand(t *testing.T) { t.Setenv("HOME", t.TempDir()) for _, args := range [][]string{ nil, {"app"}, {"app", "--schema"}, + {"phone"}, + {"phone", "serve"}, + {"phone", "--home", "/elsewhere"}, + {"phone", "other", "--home", "/elsewhere"}, {"chat", "--home", "/elsewhere"}, {"exec", "--home", "/elsewhere", "say hi"}, {"version"}, From f77c8c7298543b006cf1bb9ebb5b7beae3974a57 Mon Sep 17 00:00:00 2001 From: sama Pyb Date: Wed, 2 Dec 2026 23:17:26 +0800 Subject: [PATCH 2/2] chore(changelog): add change entry for #3022 --- docs/changes/unreleased/3022-phone-serve-home-early.md | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 docs/changes/unreleased/3022-phone-serve-home-early.md diff --git a/docs/changes/unreleased/3022-phone-serve-home-early.md b/docs/changes/unreleased/3022-phone-serve-home-early.md new file mode 100644 index 0000000000..15b84b3dd8 --- /dev/null +++ b/docs/changes/unreleased/3022-phone-serve-home-early.md @@ -0,0 +1,10 @@ +--- +kind: fixed +title: codeaf phone serve applies --home before startup reads the default home +pr: 3022 +surface: [remote] +invalidates: + - "Was true: `codeaf phone serve --home ` applied `--home` only inside `runPhoneContext`, so startup home adoption, `home.Witness`, and telemetry consent read the default state root first. Is true now: `useAppHomeEarly` applies `codeaf phone serve --home ` at the start of `main` alongside `codeaf app --home `." +--- + +`useAppHomeEarly` now parses `codeaf phone serve`'s `--home` flag (using the same `newPhoneServeFlags` and flag reordering as `runPhoneContext`) before `main` runs `home.Adopt`, `home.Witness`, or telemetry configuration.