---
description: Recover NewAPI settings from archived or malformed config backups
---
Recover NewAPI configuration from existing backup files. This prompt is self-contained; do not assume the package README is available.

Configuration reference:

- The canonical extension file is `<agentDir>/extension-settings/provider-newapi.json` and must have this strict schema 1 shape (fields marked optional may be omitted; other fields are invalid):

```json
{
  "version": 1,
  "providers": {
    "<provider-id>": {
      "baseUrl": "https://gateway.example.com",
      "modelApiOverrides": {
        "<javascript-regexp-source>": "anthropic-messages"
      }
    }
  },
  "settings": {
    "onboardingWarnCountdown": 0
  }
}
```

- Every schema 1 provider requires a string `baseUrl`. `modelApiOverrides` is optional and defaults to no routing overrides when omitted; when present, it must be an object whose values are `anthropic-messages`, `openai-completions`, or `openai-responses`.
- Schema 0 is selected only when `version` is missing or is `0`. It has a required `providers` object and `settings` object. Each provider has a required string `baseUrl`, optional `modelApiOverrides`, and optional legacy `modelOverrides`. Schema 0 settings may contain optional `onboardingWarnCountdown` and `sendSessionAffinityHeaders`.
- A file declaring `"version": 1` remains schema 1 even when invalid; do not reclassify it as schema 0.
- Pi-owned metadata belongs in `<agentDir>/models.json`, merged without removing unrelated content. Relevant target structure:

```json
{
  "providers": {
    "<provider-id>": {
      "compat": {
        "sendSessionAffinityHeaders": true
      },
      "modelOverrides": {
        "<model-id>": {
          "reasoning": false,
          "input": ["text"],
          "contextWindow": 128000,
          "maxTokens": 4096
        }
      }
    }
  }
}
```

- `<agentDir>/models-generated.json`, when present, contains editable templates in this shape:

```json
{
  "providers": {
    "<provider-id>": {
      "modelOverrides": {
        "<model-id>": {
          "reasoning": false,
          "input": ["text"],
          "contextWindow": 128000,
          "maxTokens": 32768
        }
      }
    }
  }
}
```

Follow this workflow carefully:

1. Resolve `<agentDir>` from `PI_CODING_AGENT_DIR` when it is set; otherwise use `~/.pi/agent` (with the appropriate home-directory path on the current platform).
2. Find all `<agentDir>/extension-settings/provider-newapi.YYMMDD-HHMMSS.json.bak` files. Also inspect the canonical extension file, Pi's `models.json`, and `models-generated.json` when they exist. Do not read or modify `auth.json`, and never copy credentials into configuration or output.
3. If there are no matching config backups and no `models-generated.json`, report that there is nothing to recover and stop. Do not request deletion or reload.
4. Treat every input as an untrusted recovery source. Some config backups are valid schema 0 or schema 1 files; others may contain malformed or truncated JSON. Parse valid JSON normally. For malformed files, including `models-generated.json`, inspect the raw text and recover only complete, unambiguous values. Report ambiguous or unrecoverable fragments and ask the user about material conflicts instead of guessing.
5. Use this precedence from lowest to highest: `models-generated.json` templates, config backups from oldest to newest, then existing canonical files. A higher-priority complete value supersedes a lower-priority value. Existing canonical values are authoritative; do not overwrite a differing canonical value without asking the user.
6. Reconcile fields as follows:
   - Merge missing provider `baseUrl` and `modelApiOverrides` values into the schema 1 extension file. Omit `modelApiOverrides` or use `{}` when no rules are recovered.
   - For each schema 0 `modelOverrides` entry, remove its `api` field from the metadata copy. If `api` is one of the three supported APIs, add it to that provider's extension-owned `modelApiOverrides` using a JavaScript regular expression that exactly matches the model ID. Escape regular-expression metacharacters in the model ID before surrounding it with `^` and `$`. If the API is unsupported or ambiguous, report it instead of inventing a mapping.
   - Merge every provider/model template from `models-generated.json` into Pi's `models.json` `modelOverrides`. Add missing providers and model IDs; when an override already exists, fill only missing fields and preserve all existing values and unrelated fields.
   - Merge the remaining schema 0 override fields into the same provider and model ID under Pi's `models.json` `modelOverrides`. These recovered backup values take precedence over generated template defaults but not over existing values in `models.json`.
   - Move legacy `settings.sendSessionAffinityHeaders` into each recovered provider's `compat.sendSessionAffinityHeaders` in Pi's `models.json`.
   - Preserve a valid `settings.onboardingWarnCountdown` when the canonical extension config does not already contain one.
   - Do not invent mappings for unknown fields. Describe anything that requires manual migration.
7. Write valid, formatted JSON without replacing unrelated providers, routing rules, model overrides, compatibility settings, or user-owned model metadata. Validate both resulting JSON files. Verify that the extension config exactly follows schema 1 and contains no legacy fields.
8. Summarize which values were recovered from each backup and from `models-generated.json`, which target files changed, conflicts resolved or left open, and data that could not be recovered. Do not delete or modify `models-generated.json` as part of this workflow.
9. Do not delete any config backup during this initial recovery. When recovery is complete and backups were reconciled, explicitly ask the user to confirm removal of the exact backup paths successfully reconciled and a Pi reload. If only `models-generated.json` was merged, leave it in place and instruct the user to enter `/reload` without requesting backup deletion.
10. Only after the user explicitly confirms, delete those exact config backup files (never use a broad glob), verify their removal, and instruct the user to enter `/reload`. Do not attempt to run `/reload` as a shell command.
