refactor(coding-agent): move auth storage to backend abstraction

This commit is contained in:
Mario Zechner 2026-02-17 19:57:21 +01:00
parent 0a6b0b8fb0
commit 2977c14917
21 changed files with 355 additions and 143 deletions

View file

@ -46,7 +46,7 @@ describe("AgentSession auto-compaction queue resume", () => {
const sessionManager = SessionManager.inMemory();
const settingsManager = SettingsManager.create(tempDir, tempDir);
const authStorage = new AuthStorage(join(tempDir, "auth.json"));
const authStorage = AuthStorage.create(join(tempDir, "auth.json"));
authStorage.setRuntimeApiKey("anthropic", "test-key");
const modelRegistry = new ModelRegistry(authStorage, tempDir);

View file

@ -54,7 +54,7 @@ describe.skipIf(!API_KEY)("AgentSession forking", () => {
sessionManager = noSession ? SessionManager.inMemory() : SessionManager.create(tempDir);
const settingsManager = SettingsManager.create(tempDir, tempDir);
const authStorage = new AuthStorage(join(tempDir, "auth.json"));
const authStorage = AuthStorage.create(join(tempDir, "auth.json"));
const modelRegistry = new ModelRegistry(authStorage, tempDir);
session = new AgentSession({

View file

@ -60,7 +60,7 @@ describe.skipIf(!API_KEY)("AgentSession compaction e2e", () => {
const settingsManager = SettingsManager.create(tempDir, tempDir);
// Use minimal keepRecentTokens so small test conversations have something to summarize
settingsManager.applyOverrides({ compaction: { keepRecentTokens: 1 } });
const authStorage = new AuthStorage(join(tempDir, "auth.json"));
const authStorage = AuthStorage.create(join(tempDir, "auth.json"));
const modelRegistry = new ModelRegistry(authStorage);
session = new AgentSession({

View file

@ -99,7 +99,7 @@ describe("AgentSession concurrent prompt guard", () => {
const sessionManager = SessionManager.inMemory();
const settingsManager = SettingsManager.create(tempDir, tempDir);
const authStorage = new AuthStorage(join(tempDir, "auth.json"));
const authStorage = AuthStorage.create(join(tempDir, "auth.json"));
const modelRegistry = new ModelRegistry(authStorage, tempDir);
// Set a runtime API key so validation passes
authStorage.setRuntimeApiKey("anthropic", "test-key");
@ -192,7 +192,7 @@ describe("AgentSession concurrent prompt guard", () => {
const sessionManager = SessionManager.inMemory();
const settingsManager = SettingsManager.create(tempDir, tempDir);
const authStorage = new AuthStorage(join(tempDir, "auth.json"));
const authStorage = AuthStorage.create(join(tempDir, "auth.json"));
const modelRegistry = new ModelRegistry(authStorage, tempDir);
authStorage.setRuntimeApiKey("anthropic", "test-key");

View file

@ -36,7 +36,7 @@ describe("AuthStorage", () => {
anthropic: { type: "api_key", key: "sk-ant-literal-key" },
});
authStorage = new AuthStorage(authJsonPath);
authStorage = AuthStorage.create(authJsonPath);
const apiKey = await authStorage.getApiKey("anthropic");
expect(apiKey).toBe("sk-ant-literal-key");
@ -47,7 +47,7 @@ describe("AuthStorage", () => {
anthropic: { type: "api_key", key: "!echo test-api-key-from-command" },
});
authStorage = new AuthStorage(authJsonPath);
authStorage = AuthStorage.create(authJsonPath);
const apiKey = await authStorage.getApiKey("anthropic");
expect(apiKey).toBe("test-api-key-from-command");
@ -58,7 +58,7 @@ describe("AuthStorage", () => {
anthropic: { type: "api_key", key: "!echo ' spaced-key '" },
});
authStorage = new AuthStorage(authJsonPath);
authStorage = AuthStorage.create(authJsonPath);
const apiKey = await authStorage.getApiKey("anthropic");
expect(apiKey).toBe("spaced-key");
@ -69,7 +69,7 @@ describe("AuthStorage", () => {
anthropic: { type: "api_key", key: "!printf 'line1\\nline2'" },
});
authStorage = new AuthStorage(authJsonPath);
authStorage = AuthStorage.create(authJsonPath);
const apiKey = await authStorage.getApiKey("anthropic");
expect(apiKey).toBe("line1\nline2");
@ -80,7 +80,7 @@ describe("AuthStorage", () => {
anthropic: { type: "api_key", key: "!exit 1" },
});
authStorage = new AuthStorage(authJsonPath);
authStorage = AuthStorage.create(authJsonPath);
const apiKey = await authStorage.getApiKey("anthropic");
expect(apiKey).toBeUndefined();
@ -91,7 +91,7 @@ describe("AuthStorage", () => {
anthropic: { type: "api_key", key: "!nonexistent-command-12345" },
});
authStorage = new AuthStorage(authJsonPath);
authStorage = AuthStorage.create(authJsonPath);
const apiKey = await authStorage.getApiKey("anthropic");
expect(apiKey).toBeUndefined();
@ -102,7 +102,7 @@ describe("AuthStorage", () => {
anthropic: { type: "api_key", key: "!printf ''" },
});
authStorage = new AuthStorage(authJsonPath);
authStorage = AuthStorage.create(authJsonPath);
const apiKey = await authStorage.getApiKey("anthropic");
expect(apiKey).toBeUndefined();
@ -117,7 +117,7 @@ describe("AuthStorage", () => {
anthropic: { type: "api_key", key: "TEST_AUTH_API_KEY_12345" },
});
authStorage = new AuthStorage(authJsonPath);
authStorage = AuthStorage.create(authJsonPath);
const apiKey = await authStorage.getApiKey("anthropic");
expect(apiKey).toBe("env-api-key-value");
@ -138,7 +138,7 @@ describe("AuthStorage", () => {
anthropic: { type: "api_key", key: "literal_api_key_value" },
});
authStorage = new AuthStorage(authJsonPath);
authStorage = AuthStorage.create(authJsonPath);
const apiKey = await authStorage.getApiKey("anthropic");
expect(apiKey).toBe("literal_api_key_value");
@ -149,7 +149,7 @@ describe("AuthStorage", () => {
anthropic: { type: "api_key", key: "!echo 'hello world' | tr ' ' '-'" },
});
authStorage = new AuthStorage(authJsonPath);
authStorage = AuthStorage.create(authJsonPath);
const apiKey = await authStorage.getApiKey("anthropic");
expect(apiKey).toBe("hello-world");
@ -166,7 +166,7 @@ describe("AuthStorage", () => {
anthropic: { type: "api_key", key: command },
});
authStorage = new AuthStorage(authJsonPath);
authStorage = AuthStorage.create(authJsonPath);
// Call multiple times
await authStorage.getApiKey("anthropic");
@ -188,10 +188,10 @@ describe("AuthStorage", () => {
});
// Create multiple AuthStorage instances
const storage1 = new AuthStorage(authJsonPath);
const storage1 = AuthStorage.create(authJsonPath);
await storage1.getApiKey("anthropic");
const storage2 = new AuthStorage(authJsonPath);
const storage2 = AuthStorage.create(authJsonPath);
await storage2.getApiKey("anthropic");
// Command should still have only run once
@ -208,7 +208,7 @@ describe("AuthStorage", () => {
anthropic: { type: "api_key", key: command },
});
authStorage = new AuthStorage(authJsonPath);
authStorage = AuthStorage.create(authJsonPath);
await authStorage.getApiKey("anthropic");
// Clear cache and call again
@ -226,7 +226,7 @@ describe("AuthStorage", () => {
openai: { type: "api_key", key: "!echo key-openai" },
});
authStorage = new AuthStorage(authJsonPath);
authStorage = AuthStorage.create(authJsonPath);
const keyA = await authStorage.getApiKey("anthropic");
const keyB = await authStorage.getApiKey("openai");
@ -244,7 +244,7 @@ describe("AuthStorage", () => {
anthropic: { type: "api_key", key: command },
});
authStorage = new AuthStorage(authJsonPath);
authStorage = AuthStorage.create(authJsonPath);
// Call multiple times - all should return undefined
const key1 = await authStorage.getApiKey("anthropic");
@ -269,7 +269,7 @@ describe("AuthStorage", () => {
anthropic: { type: "api_key", key: envVarName },
});
authStorage = new AuthStorage(authJsonPath);
authStorage = AuthStorage.create(authJsonPath);
const key1 = await authStorage.getApiKey("anthropic");
expect(key1).toBe("first-value");
@ -320,7 +320,7 @@ describe("AuthStorage", () => {
},
});
authStorage = new AuthStorage(authJsonPath);
authStorage = AuthStorage.create(authJsonPath);
const realLock = lockfile.lock.bind(lockfile);
const lockSpy = vi.spyOn(lockfile, "lock");
@ -339,13 +339,97 @@ describe("AuthStorage", () => {
});
});
describe("persistence semantics", () => {
test("set preserves unrelated external edits", () => {
writeAuthJson({
anthropic: { type: "api_key", key: "old-anthropic" },
openai: { type: "api_key", key: "openai-key" },
});
authStorage = AuthStorage.create(authJsonPath);
// Simulate external edit while process is running
writeAuthJson({
anthropic: { type: "api_key", key: "old-anthropic" },
openai: { type: "api_key", key: "openai-key" },
google: { type: "api_key", key: "google-key" },
});
authStorage.set("anthropic", { type: "api_key", key: "new-anthropic" });
const updated = JSON.parse(readFileSync(authJsonPath, "utf-8")) as Record<string, { key: string }>;
expect(updated.anthropic.key).toBe("new-anthropic");
expect(updated.openai.key).toBe("openai-key");
expect(updated.google.key).toBe("google-key");
});
test("remove preserves unrelated external edits", () => {
writeAuthJson({
anthropic: { type: "api_key", key: "anthropic-key" },
openai: { type: "api_key", key: "openai-key" },
});
authStorage = AuthStorage.create(authJsonPath);
// Simulate external edit while process is running
writeAuthJson({
anthropic: { type: "api_key", key: "anthropic-key" },
openai: { type: "api_key", key: "openai-key" },
google: { type: "api_key", key: "google-key" },
});
authStorage.remove("anthropic");
const updated = JSON.parse(readFileSync(authJsonPath, "utf-8")) as Record<string, { key: string }>;
expect(updated.anthropic).toBeUndefined();
expect(updated.openai.key).toBe("openai-key");
expect(updated.google.key).toBe("google-key");
});
test("does not overwrite malformed auth file after load error", () => {
writeAuthJson({
anthropic: { type: "api_key", key: "anthropic-key" },
});
authStorage = AuthStorage.create(authJsonPath);
writeFileSync(authJsonPath, "{invalid-json", "utf-8");
authStorage.reload();
authStorage.set("openai", { type: "api_key", key: "openai-key" });
const raw = readFileSync(authJsonPath, "utf-8");
expect(raw).toBe("{invalid-json");
});
test("reload records parse errors and drainErrors clears buffer", () => {
writeAuthJson({
anthropic: { type: "api_key", key: "anthropic-key" },
});
authStorage = AuthStorage.create(authJsonPath);
writeFileSync(authJsonPath, "{invalid-json", "utf-8");
authStorage.reload();
// Keeps previous in-memory data on reload failure
expect(authStorage.get("anthropic")).toEqual({ type: "api_key", key: "anthropic-key" });
const firstDrain = authStorage.drainErrors();
expect(firstDrain.length).toBeGreaterThan(0);
expect(firstDrain[0]).toBeInstanceOf(Error);
const secondDrain = authStorage.drainErrors();
expect(secondDrain).toHaveLength(0);
});
});
describe("runtime overrides", () => {
test("runtime override takes priority over auth.json", async () => {
writeAuthJson({
anthropic: { type: "api_key", key: "!echo stored-key" },
});
authStorage = new AuthStorage(authJsonPath);
authStorage = AuthStorage.create(authJsonPath);
authStorage.setRuntimeApiKey("anthropic", "runtime-key");
const apiKey = await authStorage.getApiKey("anthropic");
@ -358,7 +442,7 @@ describe("AuthStorage", () => {
anthropic: { type: "api_key", key: "!echo stored-key" },
});
authStorage = new AuthStorage(authJsonPath);
authStorage = AuthStorage.create(authJsonPath);
authStorage.setRuntimeApiKey("anthropic", "runtime-key");
authStorage.removeRuntimeApiKey("anthropic");

View file

@ -96,7 +96,7 @@ describe.skipIf(!API_KEY)("Compaction extensions", () => {
const sessionManager = SessionManager.create(tempDir);
const settingsManager = SettingsManager.create(tempDir, tempDir);
const authStorage = new AuthStorage(join(tempDir, "auth.json"));
const authStorage = AuthStorage.create(join(tempDir, "auth.json"));
const modelRegistry = new ModelRegistry(authStorage);
const runtime = createExtensionRuntime();

View file

@ -29,7 +29,7 @@ describe("Input Event", () => {
for (let i = 0; i < extensions.length; i++) fs.writeFileSync(path.join(extensionsDir, `e${i}.ts`), extensions[i]);
const result = await discoverAndLoadExtensions([], tempDir, tempDir);
const sm = SessionManager.inMemory();
const mr = new ModelRegistry(new AuthStorage(path.join(tempDir, "auth.json")));
const mr = new ModelRegistry(AuthStorage.create(path.join(tempDir, "auth.json")));
return new ExtensionRunner(result.extensions, result.runtime, tempDir, sm, mr);
}

View file

@ -24,7 +24,7 @@ describe("ExtensionRunner", () => {
extensionsDir = path.join(tempDir, "extensions");
fs.mkdirSync(extensionsDir);
sessionManager = SessionManager.inMemory();
const authStorage = new AuthStorage(path.join(tempDir, "auth.json"));
const authStorage = AuthStorage.create(path.join(tempDir, "auth.json"));
modelRegistry = new ModelRegistry(authStorage);
});

View file

@ -15,7 +15,7 @@ describe("ModelRegistry", () => {
tempDir = join(tmpdir(), `pi-test-model-registry-${Date.now()}-${Math.random().toString(36).slice(2)}`);
mkdirSync(tempDir, { recursive: true });
modelsJsonPath = join(tempDir, "models.json");
authStorage = new AuthStorage(join(tempDir, "auth.json"));
authStorage = AuthStorage.create(join(tempDir, "auth.json"));
});
afterEach(() => {

View file

@ -119,7 +119,7 @@ export const PI_AGENT_DIR = join(homedir(), ".pi", "agent");
* Use this for tests that need real OAuth credentials.
*/
export function getRealAuthStorage(): AuthStorage {
return new AuthStorage(AUTH_PATH);
return AuthStorage.create(AUTH_PATH);
}
/**
@ -214,7 +214,7 @@ export function createTestSession(options: TestSessionOptions = {}): TestSession
settingsManager.applyOverrides(options.settingsOverrides);
}
const authStorage = new AuthStorage(join(tempDir, "auth.json"));
const authStorage = AuthStorage.create(join(tempDir, "auth.json"));
const modelRegistry = new ModelRegistry(authStorage, tempDir);
const session = new AgentSession({