can1357/oh-my-pi · error

Failed to initialize authentication storage

Error message

Failed to initialize authentication storage

What it means

runSearchQuery needs an AuthStorage instance to access provider credentials and a ModelRegistry. If the caller supplies neither authStorage nor modelRegistry and discoverAuthStorage() cannot locate/create one (no stored auth on disk), it throws 'Failed to initialize authentication storage'. It is an environment/setup failure, not a search failure.

Source

Thrown at packages/coding-agent/src/web/search/index.ts:298

		},
	};
}

/**
 * Execute a web search query for CLI/testing workflows.
 *
 * `authStorage` may be omitted; in that case we discover one via the standard
 * factory (`discoverAuthStorage`), which honours `OMP_AUTH_BROKER_URL` and
 * otherwise opens the local SQLite credential store.
 */
export async function runSearchQuery(
	params: SearchQueryParams,
	options: { authStorage?: AuthStorage; modelRegistry?: ModelRegistry; sessionId?: string; signal?: AbortSignal } = {},
): Promise<{ content: Array<{ type: "text"; text: string }>; details: SearchRenderDetails }> {
	const createdAuthStorage = options.authStorage || options.modelRegistry ? undefined : await discoverAuthStorage();
	const authStorage = options.authStorage ?? options.modelRegistry?.authStorage ?? createdAuthStorage;
	if (!authStorage) {
		throw new Error("Failed to initialize authentication storage");
	}
	const modelRegistry = options.modelRegistry ?? (createdAuthStorage ? new ModelRegistry(authStorage) : undefined);
	try {
		return await executeSearch("cli-web-search", params, {
			authStorage,
			modelRegistry,
			sessionId: options.sessionId,
			signal: options.signal,
		});
	} finally {
		createdAuthStorage?.close();
	}
}

/**
 * Web search tool implementation.
 *
 * Supports the configured web-search provider chain with automatic fallback.

View on GitHub (pinned to 9690622007)

Solutions

  1. Run the app's auth/login flow once to create the auth storage on this machine/user.
  2. Pass an explicit AuthStorage (or a ModelRegistry with authStorage) via options instead of relying on discovery.
  3. Check HOME/USERPROFILE and app config directory resolution — discovery may look in the wrong place in daemons/CI.
  4. Reuse a single shared authStorage instance you create at startup and thread it through all tool calls.

Example fix

// before
await runSearchQuery(params); // relies on discoverAuthStorage()
// after
import { discoverAuthStorage } from "...";
const authStorage = await discoverAuthStorage();
if (!authStorage) throw new Error("Run `omp auth` to initialize credentials");
await runSearchQuery(params, { authStorage });
Defensive patterns

Strategy: validation

Validate before calling

const authStorage = options.authStorage ?? options.modelRegistry?.authStorage ?? (await discoverAuthStorage());
if (!authStorage) {
  throw new Error("No auth storage — run the app's auth/login flow or pass authStorage explicitly");
}

Type guard

null

Try / catch

try { return await runSearchQuery(params, opts); }
catch (e) {
  if (e instanceof Error && e.message === "Failed to initialize authentication storage") {
    // prompt user to authenticate or re-run with explicit authStorage
    return { content: [{ type: "text", text: "Authentication not initialized. Run `omp auth`." }] };
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling runSearchQuery with no options.authStorage and no options.modelRegistry while the machine has no discoverable auth storage (fresh environment, HOME not set, credentials never initialized).

Common situations: Running the web-search tool in CI or a container without prior login; running under a different user/HOME than where credentials were created; SDK/embedding usage without passing an explicit authStorage.

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31). Data as JSON: /api/errors/99b15f739cee0708. Report an issue: GitHub.