From 6f5ca6814fdca11c1ddbcdf8216bb43ef9d334c7 Mon Sep 17 00:00:00 2001 From: Silkage Date: Mon, 9 Nov 2026 10:28:03 +0800 Subject: [PATCH] fix: preserve projects across account switches --- README.md | 22 +- docs/architecture.md | 24 + docs/compatibility.md | 7 + docs/troubleshooting.md | 24 + internal/app/app.go | 124 ++++- internal/doctor/doctor.go | 11 + internal/projectstate/projectstate.go | 508 +++++++++++++++++++++ internal/projectstate/projectstate_test.go | 179 ++++++++ 8 files changed, 896 insertions(+), 3 deletions(-) create mode 100644 internal/projectstate/projectstate.go create mode 100644 internal/projectstate/projectstate_test.go diff --git a/README.md b/README.md index a594754..4f17544 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,9 @@ codex-switch current # Detect or reconcile a login performed directly in Codex. codex-switch sync --check codex-switch sync + +# Inspect Codex's local project migration after an app update. +codex-switch project status ``` Use `codex-switch doctor` before reporting a problem. Machine-readable output is @@ -149,6 +152,8 @@ codex-switch current codex-switch status codex-switch sync [--check] [--prefer-live] [--as ] codex-switch doctor +codex-switch project status +codex-switch project repair [--force] codex-switch use codex-switch deactivate codex-switch select @@ -194,6 +199,14 @@ same safe reconciliation without rewriting `auth.json`, closing Codex, or requiring a restart. A real switch to a different account still requires Codex to be stopped. +Recent Codex desktop releases moved canonical thread-to-project membership into +the App Server state database while retaining a legacy desktop-side assignment +map. If that upstream migration is incomplete, `project status` reports it and +`project repair` reconciles the legacy IDs through the official App Server +protocol. A normal `use` automatically performs this repair when Codex is +closed. The operation is idempotent and creates a timestamped backup of +`.codex-global-state.json` before marking the migration complete. + ## What a switch changes Normal account switches modify only: @@ -201,6 +214,11 @@ Normal account switches modify only: - `$CODEX_HOME/auth.json` - `codex-switch`'s own state and encrypted vault +When Codex reports an incomplete local-project migration, a switch performed +while Codex is closed may additionally restore `threads.project_id` through the +official App Server and update the backed-up desktop migration checkpoint. It +does not copy projects or sessions into per-account homes. + An account-usage query may persist an officially refreshed credential generation back to the encrypted vault. If the queried profile is active, the same generation is safely reconciled into `$CODEX_HOME/auth.json`; no account selection @@ -213,8 +231,8 @@ session into the shared `CODEX_HOME`. Initialization may make a one-time, backed-up change to `$CODEX_HOME/config.toml` to set `cli_auth_credentials_store = "file"`. -`codex-switch` does not rewrite session history or Codex configuration during a -switch. +`codex-switch` does not rewrite rollout/session history or Codex configuration +during a switch. ## Documentation diff --git a/docs/architecture.md b/docs/architecture.md index af20a01..ce44b58 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -31,6 +31,10 @@ refresh generations, and coordinates with switching through the same lock. - `usagecache` stores credential-free successful snapshots separately from the encrypted vault. +- `projectstate` detects the desktop's legacy-to-App-Server project migration, + restores canonical thread assignments through the official experimental App + Server API, and advances the desktop checkpoint only after every assignment + is confirmed. - `atomicfile` publishes complete files and refuses symlink destinations. - `doctor` reports only redacted, non-secret local facts. @@ -56,6 +60,26 @@ The journal contains only profile IDs, hashes, and timestamps. If the process stops after replacement but before state persistence, recovery compares the live file with both hashes and completes the state transition. +## Local project compatibility repair + +```text +acquire shared operation lock + -> read the desktop legacy project and thread-assignment maps + -> resolve legacy project IDs to App Server project IDs + -> require Codex to be closed + -> initialize App Server with experimental API capability + -> validate every target with project/list + -> apply and confirm thread/metadata/update(projectId) idempotently + -> back up .codex-global-state.json + -> mark thread assignment migration complete and clear its pending queue +``` + +The existing legacy assignment map remains intact because current desktop +releases still use it for sidebar ordering and reconciliation. A failed or +interrupted run does not advance the checkpoint, so the repair can safely be +retried. `--allow-running` is an expert recovery override because an open +desktop process can overwrite its state file from memory. + ## Live-state reconciliation ```text diff --git a/docs/compatibility.md b/docs/compatibility.md index 6b24f94..e72d45e 100644 --- a/docs/compatibility.md +++ b/docs/compatibility.md @@ -69,6 +69,13 @@ Usage querying initializes `codex app-server` and calls: - `account/rateLimits/read` - `account/usage/read` +Project migration repair was added for Codex CLI `0.167.0`. It negotiates the +App Server's `experimentalApi` capability and uses `project/list` plus +`thread/metadata/update` with `projectId`. Older clients are unaffected unless +their desktop state explicitly reports a pending App Server project migration; +in that case the command fails with an update hint instead of editing SQLite +directly. + `account wake` additionally requires the installed CLI's non-interactive `codex exec` mode and its `--ephemeral`, `--ignore-user-config`, `--ignore-rules`, and read-only sandbox options. The wake turn uses the CLI's diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index ec1637d..dfb5066 100644 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -53,6 +53,30 @@ credential after replacement. `--allow-running` exists for recovery and expert use. It is not recommended for ordinary switching. +## Projects are empty or change after switching accounts + +Recent Codex releases store projects and thread-to-project membership +separately. A desktop migration can create the projects successfully while +leaving every canonical thread `projectId` empty. Inspect the redacted status: + +```bash +codex-switch project status +codex-switch doctor +``` + +If repair is needed, fully quit Codex/ChatGPT and run: + +```bash +codex-switch project repair +``` + +The repair uses Codex App Server rather than editing its SQLite database. It +keeps the desktop's legacy project map, verifies the new project IDs, applies +each assignment idempotently, and backs up `.codex-global-state.json` before +advancing the migration checkpoint. Use `--force` to reapply all retained +assignments after a later Codex regression. Avoid `--allow-running` unless the +desktop cannot be stopped; its in-memory state may overwrite the repair. + ## Active account is unmanaged Inspect it and preserve it before switching: diff --git a/internal/app/app.go b/internal/app/app.go index 195dea0..1683681 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -24,6 +24,8 @@ import ( appconfig "github.com/SilkageNet/codex-switch/internal/config" "github.com/SilkageNet/codex-switch/internal/doctor" "github.com/SilkageNet/codex-switch/internal/launcher" + "github.com/SilkageNet/codex-switch/internal/process" + "github.com/SilkageNet/codex-switch/internal/projectstate" "github.com/SilkageNet/codex-switch/internal/secretstore" appstate "github.com/SilkageNet/codex-switch/internal/state" "github.com/SilkageNet/codex-switch/internal/switcher" @@ -106,6 +108,7 @@ func NewCommand(version string) *cobra.Command { newStatusCommand(options), newSyncCommand(options), newDoctorCommand(options), + newProjectCommand(options), newUseCommand(options), newDeactivateCommand(options), newSelectCommand(options), @@ -629,6 +632,28 @@ func newUseCommand(options *Options) *cobra.Command { if err != nil { return err } + var projectRepair *projectstate.Result + var projectWarning string + inspection, inspectErr := runtime.projectService(options.Version).Inspect() + if inspectErr != nil { + projectWarning = "Project migration status could not be inspected: " + inspectErr.Error() + } else if inspection.NeedsRepair { + running, detectErr := process.DetectCodex() + switch { + case detectErr != nil: + projectWarning = "Project assignment migration is pending; run 'codex-switch project repair' after closing Codex." + case allowRunning || len(running) > 0: + projectWarning = "Project assignment migration is pending; run 'codex-switch project repair' after closing Codex." + default: + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute) + repaired, repairErr := runtime.projectService(options.Version).Repair(ctx, false, false) + cancel() + if repairErr != nil { + return fmt.Errorf("account switched to %s but project assignment migration failed: %w", result.Alias, repairErr) + } + projectRepair = &repaired + } + } if launch { if err := launcher.Codex(); err != nil { return fmt.Errorf("account switched but Codex could not be launched: %w", err) @@ -641,7 +666,18 @@ func newUseCommand(options *Options) *cobra.Command { message = fmt.Sprintf("Account %s is already active. Reconciled changes made outside codex-switch; no restart is required.", result.Alias) } } - return options.render(result, message) + if projectRepair != nil && projectRepair.Repaired { + message += fmt.Sprintf(" Repaired %d local project assignment(s).", projectRepair.AssignmentsRepaired) + } + if projectWarning != "" { + message += "\nWarning: " + projectWarning + } + view := struct { + switcher.Result + ProjectMigration *projectstate.Result `json:"projectMigration,omitempty"` + Warning string `json:"warning,omitempty"` + }{Result: result, ProjectMigration: projectRepair, Warning: projectWarning} + return options.render(view, message) }, } command.Flags().BoolVar(&allowRunning, "allow-running", false, "switch even when a Codex process is detected") @@ -649,6 +685,74 @@ func newUseCommand(options *Options) *cobra.Command { return command } +func newProjectCommand(options *Options) *cobra.Command { + command := &cobra.Command{Use: "project", Aliases: []string{"projects"}, Short: "Inspect and repair shared local project state"} + command.AddCommand(newProjectStatusCommand(options), newProjectRepairCommand(options)) + return command +} + +func newProjectStatusCommand(options *Options) *cobra.Command { + return &cobra.Command{ + Use: "status", + Short: "Inspect Codex project assignment migration", + RunE: func(*cobra.Command, []string) error { + runtime, err := options.loadProjectRuntime() + if err != nil { + return err + } + inspection, err := runtime.projectService(options.Version).Inspect() + if err != nil { + return err + } + message := formatProjectInspection(inspection) + return options.render(inspection, message) + }, + } +} + +func newProjectRepairCommand(options *Options) *cobra.Command { + var allowRunning bool + var force bool + command := &cobra.Command{ + Use: "repair", + Short: "Reconcile legacy project assignments through Codex App Server", + RunE: func(*cobra.Command, []string) error { + runtime, err := options.loadProjectRuntime() + if err != nil { + return err + } + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute) + defer cancel() + result, err := runtime.projectService(options.Version).Repair(ctx, allowRunning, force) + if err != nil { + return err + } + message := "Project assignments are already consistent." + if result.Repaired { + message = fmt.Sprintf("Repaired %d local project assignment(s). Backup: %s", result.AssignmentsRepaired, result.Backup) + } + return options.render(result, message) + }, + } + command.Flags().BoolVar(&allowRunning, "allow-running", false, "repair while Codex is running (may be overwritten by the desktop app)") + command.Flags().BoolVar(&force, "force", false, "reapply every resolvable legacy project assignment") + return command +} + +func formatProjectInspection(inspection projectstate.Inspection) string { + if !inspection.Available { + return "Codex has no local app-server project migration state for this CODEX_HOME." + } + status := "complete" + if inspection.NeedsRepair { + status = "repair needed" + } else if !inspection.ThreadAssignmentsMigrated { + status = "not applicable" + } + return fmt.Sprintf("Projects: %d\nAssignments: %d (%d resolvable, %d pending)\nMigration: %s", + inspection.Projects, inspection.LegacyAssignments, inspection.ResolvableAssignments, inspection.PendingAssignments, status) +} + func newDeactivateCommand(options *Options) *cobra.Command { var allowRunning bool command := &cobra.Command{ @@ -1029,10 +1133,28 @@ func (options *Options) loadRuntimeForDoctor() (runtimeState, error) { return runtimeState{home: home, paths: paths, manager: vault.New(paths.Vault, store), bin: bin, binErr: binErr}, nil } +func (options *Options) loadProjectRuntime() (runtimeState, error) { + home, err := codexhome.Resolve(options.CodexHome) + if err != nil { + return runtimeState{}, err + } + bin, binErr := codexlogin.FindBinary(options.CodexBin) + return runtimeState{home: home, bin: bin, binErr: binErr}, nil +} + func (runtime runtimeState) switcher() switcher.Service { return switcher.Service{Home: runtime.home, Paths: runtime.paths, Vault: runtime.manager} } +func (runtime runtimeState) projectService(version string) projectstate.Service { + return projectstate.Service{ + Home: runtime.home, + Binary: runtime.bin, + BinaryError: runtime.binErr, + ClientVersion: version, + } +} + func (runtime runtimeState) usageService(version string) accountusage.Service { return accountusage.Service{ Home: runtime.home, diff --git a/internal/doctor/doctor.go b/internal/doctor/doctor.go index dbc8528..a38ecc8 100644 --- a/internal/doctor/doctor.go +++ b/internal/doctor/doctor.go @@ -11,6 +11,7 @@ import ( "github.com/SilkageNet/codex-switch/internal/codexlogin" appconfig "github.com/SilkageNet/codex-switch/internal/config" "github.com/SilkageNet/codex-switch/internal/process" + "github.com/SilkageNet/codex-switch/internal/projectstate" "github.com/SilkageNet/codex-switch/internal/switcher" "github.com/SilkageNet/codex-switch/internal/vault" ) @@ -43,6 +44,16 @@ func Run(home codexhome.Home, paths appconfig.Paths, manager *vault.Manager, cod add("codex_home", "error", fmt.Sprintf("cannot inspect %s: %v", home.Path, err)) } + if inspection, err := (projectstate.Service{Home: home}).Inspect(); err != nil { + add("project_assignments", "warning", err.Error()) + } else if !inspection.Available { + add("project_assignments", "ok", "no local app-server migration state") + } else if inspection.NeedsRepair { + add("project_assignments", "warning", fmt.Sprintf("%d assignment(s) pending; run codex-switch project repair after closing Codex", inspection.PendingAssignments)) + } else { + add("project_assignments", "ok", fmt.Sprintf("%d assignment(s), migration complete", inspection.ResolvableAssignments)) + } + if mode, err := home.CredentialStore(); err != nil { add("credential_store", "error", err.Error()) } else if mode != codexhome.StoreFile { diff --git a/internal/projectstate/projectstate.go b/internal/projectstate/projectstate.go new file mode 100644 index 0000000..ad31d31 --- /dev/null +++ b/internal/projectstate/projectstate.go @@ -0,0 +1,508 @@ +package projectstate + +import ( + "bufio" + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "io" + "os" + "os/exec" + "path/filepath" + "sort" + "strconv" + "strings" + "time" + + "github.com/SilkageNet/codex-switch/internal/atomicfile" + "github.com/SilkageNet/codex-switch/internal/codexhome" + "github.com/SilkageNet/codex-switch/internal/filelock" + "github.com/SilkageNet/codex-switch/internal/process" +) + +const ( + globalStateName = ".codex-global-state.json" + maxGlobalStateSize = 32 << 20 + maxProtocolMessage = 8 << 20 +) + +type Inspection struct { + Available bool `json:"available"` + HostKey string `json:"hostKey,omitempty"` + Projects int `json:"projects"` + LegacyAssignments int `json:"legacyAssignments"` + ResolvableAssignments int `json:"resolvableAssignments"` + PendingAssignments int `json:"pendingAssignments"` + ProjectsMigrated bool `json:"projectsMigrated"` + ThreadAssignmentsMigrated bool `json:"threadAssignmentsMigrated"` + NeedsRepair bool `json:"needsRepair"` +} + +type Result struct { + Inspection + Repaired bool `json:"repaired"` + AssignmentsRepaired int `json:"assignmentsRepaired"` + Backup string `json:"backup,omitempty"` +} + +type Service struct { + Home codexhome.Home + Binary string + BinaryError error + ClientVersion string + Runner *Runner +} + +type assignment struct { + ProjectKind string `json:"projectKind"` + ProjectID string `json:"projectId"` +} + +type migration struct { + Version int `json:"version"` + ProjectsMigrated bool `json:"projectsMigrated"` + ThreadAssignmentsMigrated bool `json:"threadAssignmentsMigrated"` + PendingThreadAssignmentIDs []string `json:"pendingThreadAssignmentIds"` +} + +type document struct { + raw map[string]json.RawMessage + originalHash string + statePath string + hostKey string + projects map[string]json.RawMessage + assignments map[string]assignment + projectIDs map[string]string + migrations map[string]migration + inspection Inspection + resolved []resolvedAssignment +} + +type resolvedAssignment struct { + ThreadID string + ProjectID string +} + +func (service Service) Inspect() (Inspection, error) { + document, err := load(service.Home.Path) + if err != nil { + if errors.Is(err, os.ErrNotExist) { + return Inspection{}, nil + } + return Inspection{}, err + } + return document.inspection, nil +} + +func (service Service) Repair(ctx context.Context, allowRunning, force bool) (Result, error) { + lock, err := filelock.Acquire(filepath.Join(service.Home.Path, ".codex-switch.lock")) + if err != nil { + return Result{}, err + } + defer func() { _ = lock.Close() }() + + document, err := load(service.Home.Path) + if err != nil { + if errors.Is(err, os.ErrNotExist) { + return Result{}, nil + } + return Result{}, err + } + result := Result{Inspection: document.inspection} + if !force && !document.inspection.NeedsRepair { + return result, nil + } + if len(document.resolved) == 0 { + return result, errors.New("no resolvable local project assignments were found") + } + if !allowRunning { + running, detectErr := process.DetectCodex() + if detectErr != nil { + return result, fmt.Errorf("detect running Codex processes: %w", detectErr) + } + if len(running) > 0 { + return result, fmt.Errorf("codex is still running (PID %d, %s); close it before repairing project assignments or explicitly pass --allow-running", running[0].PID, running[0].Command) + } + } + if service.BinaryError != nil { + return result, service.BinaryError + } + if service.Binary == "" { + return result, errors.New("official Codex executable not found; install Codex or pass --codex-binary") + } + + runner := service.Runner + if runner == nil { + runner = &Runner{Binary: service.Binary, ClientVersion: service.ClientVersion} + } + if err := runner.Assign(ctx, service.Home.Path, document.resolved); err != nil { + return result, err + } + backup, err := document.markMigrated() + if err != nil { + return result, fmt.Errorf("project assignments were repaired but the migration checkpoint could not be saved: %w", err) + } + result.Repaired = true + result.AssignmentsRepaired = len(document.resolved) + result.Backup = backup + result.ThreadAssignmentsMigrated = true + result.PendingAssignments = 0 + result.NeedsRepair = false + return result, nil +} + +func load(home string) (document, error) { + statePath := filepath.Join(home, globalStateName) + data, err := atomicfile.ReadLimited(statePath, maxGlobalStateSize) + if err != nil { + return document{}, err + } + var raw map[string]json.RawMessage + if err := json.Unmarshal(data, &raw); err != nil { + return document{}, fmt.Errorf("decode %s: %w", statePath, err) + } + var projects map[string]json.RawMessage + if value := raw["local-projects"]; len(value) > 0 { + if err := json.Unmarshal(value, &projects); err != nil { + return document{}, fmt.Errorf("decode local projects: %w", err) + } + } + var assignments map[string]assignment + if value := raw["thread-project-assignments"]; len(value) > 0 { + if err := json.Unmarshal(value, &assignments); err != nil { + return document{}, fmt.Errorf("decode thread project assignments: %w", err) + } + } + var projectIDsByHost map[string]map[string]string + if value := raw["app-server-project-id-by-legacy-project-id-by-host"]; len(value) > 0 { + if err := json.Unmarshal(value, &projectIDsByHost); err != nil { + return document{}, fmt.Errorf("decode project ID mappings: %w", err) + } + } + var migrations map[string]migration + if value := raw["app-server-projects-migration-by-host"]; len(value) > 0 { + if err := json.Unmarshal(value, &migrations); err != nil { + return document{}, fmt.Errorf("decode project migration state: %w", err) + } + } + hostKey := "local:" + home + projectIDs := projectIDsByHost[hostKey] + migrationState, available := migrations[hostKey] + resolved := make([]resolvedAssignment, 0, len(assignments)) + for threadID, legacy := range assignments { + if legacy.ProjectKind != "local" || strings.TrimSpace(threadID) == "" { + continue + } + projectID := projectIDs[legacy.ProjectID] + if projectID == "" { + continue + } + resolved = append(resolved, resolvedAssignment{ThreadID: threadID, ProjectID: projectID}) + } + sort.Slice(resolved, func(first, second int) bool { + return resolved[first].ThreadID < resolved[second].ThreadID + }) + inspection := Inspection{ + Available: available, + HostKey: hostKey, + Projects: len(projects), + LegacyAssignments: len(assignments), + ResolvableAssignments: len(resolved), + PendingAssignments: len(migrationState.PendingThreadAssignmentIDs), + ProjectsMigrated: migrationState.ProjectsMigrated, + ThreadAssignmentsMigrated: migrationState.ThreadAssignmentsMigrated, + } + inspection.NeedsRepair = available && migrationState.ProjectsMigrated && !migrationState.ThreadAssignmentsMigrated && len(resolved) > 0 + return document{ + raw: raw, + originalHash: atomicfile.Hash(data), + statePath: statePath, + hostKey: hostKey, + projects: projects, + assignments: assignments, + projectIDs: projectIDs, + migrations: migrations, + inspection: inspection, + resolved: resolved, + }, nil +} + +func (document *document) markMigrated() (string, error) { + migrationState, ok := document.migrations[document.hostKey] + if !ok { + return "", errors.New("local project migration checkpoint disappeared") + } + migrationState.ThreadAssignmentsMigrated = true + migrationState.PendingThreadAssignmentIDs = []string{} + document.migrations[document.hostKey] = migrationState + encodedMigrations, err := json.Marshal(document.migrations) + if err != nil { + return "", err + } + document.raw["app-server-projects-migration-by-host"] = encodedMigrations + output, err := json.Marshal(document.raw) + if err != nil { + return "", err + } + output = append(output, '\n') + original, err := atomicfile.ReadLimited(document.statePath, maxGlobalStateSize) + if err != nil { + return "", err + } + if atomicfile.Hash(original) != document.originalHash { + return "", errors.New("codex global state changed during project repair; the assignment updates are safe, but the migration checkpoint was not advanced; close Codex and retry") + } + backup := fmt.Sprintf("%s.codex-switch.bak.%s", document.statePath, time.Now().UTC().Format("20060102T150405.000000000Z")) + if err := atomicfile.Write(backup, original, 0o600); err != nil { + return "", fmt.Errorf("back up global state: %w", err) + } + if err := atomicfile.Write(document.statePath, output, 0o600); err != nil { + return "", fmt.Errorf("save global state: %w", err) + } + return backup, nil +} + +type Runner struct { + Binary string + ClientVersion string + command func(context.Context, string, ...string) *exec.Cmd +} + +type rpcRequest struct { + Method string `json:"method"` + ID *int `json:"id,omitempty"` + Params any `json:"params,omitempty"` +} + +type rpcResponse struct { + ID json.RawMessage `json:"id"` + Result json.RawMessage `json:"result"` + Error *rpcError `json:"error"` +} + +type rpcError struct { + Code int `json:"code"` + Message string `json:"message"` +} + +type projectListResponse struct { + Data []struct { + ID string `json:"id"` + } `json:"data"` + NextCursor *string `json:"nextCursor"` +} + +type threadUpdateResponse struct { + Thread struct { + ID string `json:"id"` + ProjectID *string `json:"projectId"` + } `json:"thread"` +} + +func (runner Runner) Assign(ctx context.Context, home string, assignments []resolvedAssignment) error { + commandFactory := runner.command + if commandFactory == nil { + commandFactory = exec.CommandContext + } + command := commandFactory(ctx, runner.Binary, "app-server") + environment := command.Env + if environment == nil { + environment = os.Environ() + } + command.Env = replaceEnvironment(environment, "CODEX_HOME", home) + stdin, err := command.StdinPipe() + if err != nil { + return err + } + stdout, err := command.StdoutPipe() + if err != nil { + return err + } + var stderr limitedBuffer + command.Stderr = &stderr + if err := command.Start(); err != nil { + return fmt.Errorf("start Codex app server: %w", err) + } + waited := false + defer func() { + _ = stdin.Close() + if !waited && command.Process != nil { + _ = command.Process.Kill() + _ = command.Wait() + } + }() + encoder := json.NewEncoder(stdin) + scanner := bufio.NewScanner(stdout) + scanner.Buffer(make([]byte, 64<<10), maxProtocolMessage) + version := runner.ClientVersion + if version == "" { + version = "dev" + } + if err := sendRequest(encoder, 0, "initialize", map[string]any{ + "clientInfo": map[string]string{"name": "codex_switch", "title": "codex-switch", "version": version}, + "capabilities": map[string]bool{"experimentalApi": true}, + }); err != nil { + return err + } + response, err := readResponse(scanner, 0) + if err != nil { + return protocolError("initialize", err, stderr.String()) + } + if response.Error != nil { + return responseError("initialize", response.Error) + } + if err := encoder.Encode(rpcRequest{Method: "initialized", Params: map[string]any{}}); err != nil { + return fmt.Errorf("send initialized notification: %w", err) + } + + projects := map[string]bool{} + nextID := 1 + var cursor *string + for { + params := map[string]any{"limit": 100} + if cursor != nil { + params["cursor"] = *cursor + } + if err := sendRequest(encoder, nextID, "project/list", params); err != nil { + return err + } + response, err = readResponse(scanner, nextID) + if err != nil { + return protocolError("list projects", err, stderr.String()) + } + if response.Error != nil { + return responseError("project/list", response.Error) + } + var page projectListResponse + if err := json.Unmarshal(response.Result, &page); err != nil { + return fmt.Errorf("decode project/list response: %w", err) + } + for _, project := range page.Data { + projects[project.ID] = true + } + nextID++ + cursor = page.NextCursor + if cursor == nil || *cursor == "" { + break + } + } + for _, item := range assignments { + if !projects[item.ProjectID] { + return fmt.Errorf("mapped project %s for thread %s is not present in Codex", item.ProjectID, item.ThreadID) + } + } + for index, item := range assignments { + requestID := nextID + index + if err := sendRequest(encoder, requestID, "thread/metadata/update", map[string]string{ + "threadId": item.ThreadID, + "projectId": item.ProjectID, + }); err != nil { + return err + } + response, err = readResponse(scanner, requestID) + if err != nil { + return protocolError("update thread project", err, stderr.String()) + } + if response.Error != nil { + return fmt.Errorf("repair project assignment for thread %s: %w", item.ThreadID, responseError("thread/metadata/update", response.Error)) + } + var updated threadUpdateResponse + if err := json.Unmarshal(response.Result, &updated); err != nil { + return fmt.Errorf("decode thread/metadata/update response: %w", err) + } + if updated.Thread.ID != item.ThreadID || updated.Thread.ProjectID == nil || *updated.Thread.ProjectID != item.ProjectID { + return fmt.Errorf("codex did not confirm project assignment for thread %s", item.ThreadID) + } + } + if err := stdin.Close(); err != nil { + return err + } + if err := command.Wait(); err != nil { + waited = true + if ctx.Err() != nil { + return fmt.Errorf("repair Codex projects: %w", ctx.Err()) + } + return protocolError("Codex app server exited", err, stderr.String()) + } + waited = true + return nil +} + +func sendRequest(encoder *json.Encoder, id int, method string, params any) error { + if err := encoder.Encode(rpcRequest{Method: method, ID: &id, Params: params}); err != nil { + return fmt.Errorf("send %s request: %w", method, err) + } + return nil +} + +func readResponse(scanner *bufio.Scanner, expected int) (rpcResponse, error) { + for scanner.Scan() { + line := bytes.TrimSpace(scanner.Bytes()) + if len(line) == 0 { + continue + } + var response rpcResponse + if err := json.Unmarshal(line, &response); err != nil { + return rpcResponse{}, fmt.Errorf("decode JSON-RPC message: %w", err) + } + if len(response.ID) == 0 || bytes.Equal(response.ID, []byte("null")) { + continue + } + id, err := strconv.Atoi(strings.Trim(string(response.ID), `"`)) + if err == nil && id == expected { + return response, nil + } + } + if err := scanner.Err(); err != nil { + return rpcResponse{}, err + } + return rpcResponse{}, io.EOF +} + +func responseError(method string, rpcErr *rpcError) error { + if rpcErr == nil { + return nil + } + return fmt.Errorf("%s failed (%d): %s", method, rpcErr.Code, rpcErr.Message) +} + +func protocolError(operation string, err error, stderr string) error { + diagnostic := strings.TrimSpace(stderr) + if diagnostic == "" { + return fmt.Errorf("%s: %w", operation, err) + } + return fmt.Errorf("%s: %w (Codex app-server diagnostics redacted)", operation, err) +} + +func replaceEnvironment(environment []string, key, value string) []string { + prefix := key + "=" + result := make([]string, 0, len(environment)+1) + for _, entry := range environment { + if !strings.HasPrefix(entry, prefix) { + result = append(result, entry) + } + } + return append(result, prefix+value) +} + +type limitedBuffer struct { + data []byte +} + +func (buffer *limitedBuffer) Write(data []byte) (int, error) { + const limit = 64 << 10 + remaining := limit - len(buffer.data) + if remaining > 0 { + if remaining > len(data) { + remaining = len(data) + } + buffer.data = append(buffer.data, data[:remaining]...) + } + return len(data), nil +} + +func (buffer *limitedBuffer) String() string { + return string(buffer.data) +} diff --git a/internal/projectstate/projectstate_test.go b/internal/projectstate/projectstate_test.go new file mode 100644 index 0000000..6a692ef --- /dev/null +++ b/internal/projectstate/projectstate_test.go @@ -0,0 +1,179 @@ +package projectstate + +import ( + "bufio" + "context" + "encoding/json" + "fmt" + "os" + "os/exec" + "path/filepath" + "testing" + "time" + + "github.com/SilkageNet/codex-switch/internal/atomicfile" + "github.com/SilkageNet/codex-switch/internal/codexhome" +) + +func TestInspectAndRepairProjectAssignments(t *testing.T) { + homePath := t.TempDir() + writeGlobalState(t, homePath) + service := Service{ + Home: codexhome.Home{Path: homePath}, + Binary: "codex", + ClientVersion: "test", + Runner: &Runner{Binary: "codex", command: func(context.Context, string, ...string) *exec.Cmd { + command := exec.Command(os.Args[0], "-test.run=TestProjectStateHelperProcess", "--") + command.Env = append(os.Environ(), "GO_WANT_PROJECT_STATE_HELPER=1") + return command + }}, + } + + inspection, err := service.Inspect() + if err != nil { + t.Fatal(err) + } + if !inspection.NeedsRepair || inspection.Projects != 1 || inspection.ResolvableAssignments != 2 || inspection.PendingAssignments != 1 { + t.Fatalf("unexpected inspection: %#v", inspection) + } + + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + result, err := service.Repair(ctx, true, false) + if err != nil { + t.Fatal(err) + } + if !result.Repaired || result.AssignmentsRepaired != 2 || result.Backup == "" { + t.Fatalf("unexpected repair result: %#v", result) + } + if _, err := os.Stat(result.Backup); err != nil { + t.Fatalf("backup was not created: %v", err) + } + + after, err := service.Inspect() + if err != nil { + t.Fatal(err) + } + if after.NeedsRepair || !after.ThreadAssignmentsMigrated || after.PendingAssignments != 0 { + t.Fatalf("migration checkpoint was not updated: %#v", after) + } + data, err := atomicfile.ReadLimited(filepath.Join(homePath, globalStateName), maxGlobalStateSize) + if err != nil { + t.Fatal(err) + } + var raw map[string]json.RawMessage + if err := json.Unmarshal(data, &raw); err != nil { + t.Fatal(err) + } + if string(raw["unrelated-setting"]) != `{"kept":true}` { + t.Fatalf("unrelated state was not preserved: %s", raw["unrelated-setting"]) + } +} + +func TestRepairIsNoopAfterMigration(t *testing.T) { + homePath := t.TempDir() + writeGlobalState(t, homePath) + statePath := filepath.Join(homePath, globalStateName) + document, err := load(homePath) + if err != nil { + t.Fatal(err) + } + if _, err := document.markMigrated(); err != nil { + t.Fatal(err) + } + before, err := os.ReadFile(statePath) + if err != nil { + t.Fatal(err) + } + result, err := (Service{Home: codexhome.Home{Path: homePath}}).Repair(context.Background(), true, false) + if err != nil { + t.Fatal(err) + } + if result.Repaired { + t.Fatalf("unexpected repair: %#v", result) + } + after, err := os.ReadFile(statePath) + if err != nil { + t.Fatal(err) + } + if string(before) != string(after) { + t.Fatal("no-op repair changed global state") + } +} + +func TestProjectStateHelperProcess(t *testing.T) { + if os.Getenv("GO_WANT_PROJECT_STATE_HELPER") != "1" { + return + } + encoder := json.NewEncoder(os.Stdout) + scanner := bufio.NewScanner(os.Stdin) + for scanner.Scan() { + var request struct { + Method string `json:"method"` + ID *int `json:"id"` + Params json.RawMessage `json:"params"` + } + if err := json.Unmarshal(scanner.Bytes(), &request); err != nil { + os.Exit(2) + } + if request.ID == nil { + continue + } + var result any + switch request.Method { + case "initialize": + result = map[string]any{"userAgent": "test"} + case "project/list": + result = map[string]any{"data": []map[string]string{{"id": "server-project"}}, "nextCursor": nil} + case "thread/metadata/update": + var params struct { + ThreadID string `json:"threadId"` + ProjectID string `json:"projectId"` + } + if err := json.Unmarshal(request.Params, ¶ms); err != nil { + os.Exit(3) + } + result = map[string]any{"thread": map[string]string{"id": params.ThreadID, "projectId": params.ProjectID}} + default: + _ = encoder.Encode(map[string]any{"id": *request.ID, "error": map[string]any{"code": -32601, "message": "unknown method"}}) + continue + } + if err := encoder.Encode(map[string]any{"id": *request.ID, "result": result}); err != nil { + os.Exit(4) + } + } + os.Exit(0) +} + +func writeGlobalState(t *testing.T, homePath string) { + t.Helper() + hostKey := "local:" + homePath + state := map[string]any{ + "local-projects": map[string]any{ + "legacy-project": map[string]any{"id": "legacy-project", "name": "Daily", "rootPaths": []string{homePath}}, + }, + "thread-project-assignments": map[string]any{ + "thread-a": map[string]string{"projectKind": "local", "projectId": "legacy-project"}, + "thread-b": map[string]string{"projectKind": "local", "projectId": "legacy-project"}, + }, + "app-server-project-id-by-legacy-project-id-by-host": map[string]any{ + hostKey: map[string]string{"legacy-project": "server-project"}, + }, + "app-server-projects-migration-by-host": map[string]any{ + hostKey: map[string]any{ + "version": 1, + "projectsMigrated": true, + "threadAssignmentsMigrated": false, + "pendingThreadAssignmentIds": []string{"thread-a"}, + }, + }, + "unrelated-setting": map[string]bool{"kept": true}, + } + data, err := json.Marshal(state) + if err != nil { + t.Fatal(err) + } + if err := atomicfile.Write(filepath.Join(homePath, globalStateName), append(data, '\n'), 0o600); err != nil { + t.Fatal(fmt.Errorf("write test global state: %w", err)) + } +}