plandex-ai/plandex · error

invalid context index: %s

Error message

invalid context index: %s

What it means

When the argument to `plandex contexts show` parses as an integer, the CLI treats it as a 1-based list position, decrements it, and bounds-checks it against the fetched context list. If the resulting index is negative (i.e. you passed 0 or a negative number) or >= the number of contexts, it throws 'invalid context index: %s'. This is purely a client-side bounds error; no context was fetched.

Source

Thrown at app/cli/cmd/context_show.go:42

		lib.MustResolveProject()

		nameOrIndex := args[0]

		// Get list of contexts first
		contexts, err := api.Client.ListContext(lib.CurrentPlanId, lib.CurrentBranch)
		if err != nil {
			log.Printf("Error listing contexts: %v\n", err)
			return fmt.Errorf("error listing contexts: %v", err)
		}

		var contextId string

		// Try parsing as index first
		if idx, err := strconv.Atoi(nameOrIndex); err == nil {
			// Convert to 0-based index
			idx--
			if idx < 0 || idx >= len(contexts) {
				return fmt.Errorf("invalid context index: %s", nameOrIndex)
			}
			contextId = contexts[idx].Id
		} else {
			// Try finding by name
			found := false
			for _, ctx := range contexts {
				if ctx.Name == nameOrIndex || ctx.FilePath == nameOrIndex {
					contextId = ctx.Id
					found = true
					break
				}
			}
			if !found {
				return fmt.Errorf("no context found with name: %s", nameOrIndex)
			}
		}

		res, apiErr := api.Client.GetContextBody(lib.CurrentPlanId, lib.CurrentBranch, contextId)

View on GitHub (pinned to e2d772072e)

Solutions

  1. Run `plandex contexts` (or `plandex contexts ls`) to see the current 1-based list, then use an index between 1 and the list length.
  2. If you intended a name, pass the context name or file path instead of a number (non-numeric args are matched by name).
  3. Verify you are on the expected plan/branch; switch with `plandex checkout` if the list you referenced belongs to another branch.
  4. Re-add the missing context with `plandex load` if it was removed.

Example fix

// before
plandex contexts show 0
// after
plandex contexts          # list contexts to get valid 1-based indexes
plandex contexts show 1
Defensive patterns

Strategy: validation

Validate before calling

# resolve a valid index before showing
INDEX=$(plandex contexts | grep -n 'my-context' | cut -d: -f1)
if [ -n "$INDEX" ] && [ "$INDEX" -ge 1 ]; then
  plandex contexts show "$INDEX"
fi

Type guard

func isValidContextIndex(arg string, count int) (int, bool) {
    idx, err := strconv.Atoi(arg)
    if err != nil {
        return 0, false
    }
    idx-- // list is 1-based
    if idx < 0 || idx >= count {
        return 0, false
    }
    return idx, true
}

Try / catch

if idx, ok := isValidContextIndex(arg, len(contexts)); ok {
    ctx := contexts[idx]
    _ = ctx
} else {
    return fmt.Errorf("index out of range 1..%d: %s", len(contexts), arg)
}

Prevention

When it happens

Trigger: Running `plandex contexts show 0` or `plandex contexts show -1` (any value <= 0), or running `plandex contexts show N` where N is larger than the number of contexts in the current plan/branch (e.g. `show 5` when only 3 contexts exist).

Common situations: Stale numbering: the user copied an index from an earlier `plandex contexts` listing and contexts have since been removed; forgetting the list is 1-based and passing 0; running in a different plan/branch than where the index was noted.

Related errors


AI-assisted analysis of plandex-ai/plandex@e2d772072e (2026-09-05). Data as JSON: /api/errors/a3c6df5f46b86842. Report an issue: GitHub.