diff --git a/cmd/pipelines-server/banner.go b/cmd/pipelines-server/banner.go index 01f905ad..288e0903 100644 --- a/cmd/pipelines-server/banner.go +++ b/cmd/pipelines-server/banner.go @@ -8,6 +8,7 @@ import ( "strings" "git.a-wan.net/alan/pipelines/internal/buildinfo" + "git.a-wan.net/alan/pipelines/internal/claude" "git.a-wan.net/alan/pipelines/internal/harness" "git.a-wan.net/alan/pipelines/internal/protocol" ) @@ -24,6 +25,8 @@ type startup struct { Harnesses harness.ListView HarnessErr error WebURL string + // ClaudeToken reports a service token for Claude Code chats. + ClaudeToken bool } // banner writes the server and backend summary. `pipelines start` prints the @@ -72,6 +75,11 @@ func banner(w io.Writer, s startup) { } else { row("Chat", strings.Join(s.Chat, ", ")) } + if s.ClaudeToken { + row("Claude", "service token present") + } else { + row("Claude", "no service token; as a background service run `claude setup-token` and save it to ~/.config/pipelines/claude-token (chmod 600)") + } if s.HarnessErr != nil { row("Harnesses", "unreadable: "+s.HarnessErr.Error()) } else if len(s.Harnesses.Profiles) == 0 { @@ -119,5 +127,6 @@ func firstNonEmpty(values ...string) string { func printBanner(s startup) { s.Harnesses, s.HarnessErr = harness.List() s.WebURL = os.Getenv("PIPELINES_WEB_PUBLIC_URL") + s.ClaudeToken = claude.HasServiceToken() banner(os.Stdout, s) } diff --git a/internal/claude/process.go b/internal/claude/process.go index 2fae635d..fecc85c3 100644 --- a/internal/claude/process.go +++ b/internal/claude/process.go @@ -82,7 +82,7 @@ func (s *session) spawn(ctx context.Context) (*process, error) { } cmd := exec.CommandContext(s.chat.ctx, s.chat.binary, args...) cmd.Dir = s.directory - cmd.Env = os.Environ() + cmd.Env = withServiceToken(os.Environ()) cmd.WaitDelay = 5 * time.Second stdin, err := cmd.StdinPipe() if err != nil { diff --git a/internal/claude/service_token.go b/internal/claude/service_token.go new file mode 100644 index 00000000..0e186095 --- /dev/null +++ b/internal/claude/service_token.go @@ -0,0 +1,62 @@ +package claude + +import ( + "os" + "path/filepath" + "strings" +) + +// ServiceTokenEnvKey names a file holding a long-lived Claude Code token +// (`claude setup-token`). Default: ~/.config/pipelines/claude-token. +const ServiceTokenEnvKey = "PIPELINES_CLAUDE_TOKEN_FILE" + +// serviceTokenPath is the private token file the server hands to the Claude +// Code processes it starts. A server running as a background service (launchd, +// systemd) cannot rely on the interactive login a terminal uses, so a token +// made for this purpose keeps its chats signed in. +func serviceTokenPath() string { + if path := strings.TrimSpace(os.Getenv(ServiceTokenEnvKey)); path != "" { + return path + } + home, err := os.UserHomeDir() + if err != nil { + return "" + } + return filepath.Join(home, ".config", "pipelines", "claude-token") +} + +// readServiceToken returns the token from a private (owner-only) regular +// file, or "" when there is none. +func readServiceToken(path string) string { + if path == "" { + return "" + } + info, err := os.Lstat(path) + if err != nil || !info.Mode().IsRegular() || info.Mode().Perm()&0o077 != 0 { + return "" + } + data, err := os.ReadFile(path) + if err != nil { + return "" + } + return strings.TrimSpace(string(data)) +} + +// withServiceToken adds the service token to [env] unless the environment +// already names one. +func withServiceToken(env []string) []string { + for _, item := range env { + if strings.HasPrefix(item, "CLAUDE_CODE_OAUTH_TOKEN=") { + return env + } + } + token := readServiceToken(serviceTokenPath()) + if token == "" { + return env + } + return append(env, "CLAUDE_CODE_OAUTH_TOKEN="+token) +} + +// HasServiceToken reports whether a usable service token is configured, for +// the server's startup report. +func HasServiceToken() bool { return readServiceToken(serviceTokenPath()) != "" } diff --git a/internal/claude/service_token_test.go b/internal/claude/service_token_test.go new file mode 100644 index 00000000..7a46b9bb --- /dev/null +++ b/internal/claude/service_token_test.go @@ -0,0 +1,35 @@ +package claude + +import ( + "os" + "path/filepath" + "slices" + "testing" +) + +func TestServiceTokenIsPassedOnlyFromAPrivateFile(t *testing.T) { + path := filepath.Join(t.TempDir(), "claude-token") + t.Setenv(ServiceTokenEnvKey, path) + base := []string{"HOME=/home/x"} + + if got := withServiceToken(base); !slices.Equal(got, base) { + t.Fatalf("no file: env = %v", got) + } + if err := os.WriteFile(path, []byte("sk-test-token\n"), 0o644); err != nil { + t.Fatal(err) + } + if got := withServiceToken(base); !slices.Equal(got, base) { + t.Fatalf("readable by others: env = %v", got) + } + if err := os.Chmod(path, 0o600); err != nil { + t.Fatal(err) + } + got := withServiceToken(base) + if !slices.Contains(got, "CLAUDE_CODE_OAUTH_TOKEN=sk-test-token") || !HasServiceToken() { + t.Fatalf("private file: env = %v", got) + } + explicit := []string{"CLAUDE_CODE_OAUTH_TOKEN=mine"} + if got := withServiceToken(explicit); !slices.Equal(got, explicit) { + t.Fatalf("explicit token replaced: %v", got) + } +}