fix(coding-agent): harden project trust persistence

This commit is contained in:
Armin Ronacher
2026-06-03 08:30:45 +02:00
Unverified
parent e4132d75d8
commit 85c052dba3
4 changed files with 104 additions and 17 deletions
@@ -268,6 +268,20 @@ describe("SettingsManager", () => {
expect(manager.getTheme()).toBe("global");
expect(manager.getProjectSettings()).toEqual({});
});
it("should fail project settings writes when project config is not trusted", async () => {
const projectSettingsPath = join(projectDir, ".pi", "settings.json");
writeFileSync(projectSettingsPath, JSON.stringify({ packages: ["npm:existing"] }));
const manager = SettingsManager.create(projectDir, agentDir, { projectConfigTrusted: false });
expect(() => manager.setProjectPackages(["npm:new"])).toThrow(
"Project config is not trusted; refusing to write project settings",
);
await manager.flush();
expect(manager.getProjectSettings()).toEqual({});
expect(JSON.parse(readFileSync(projectSettingsPath, "utf-8"))).toEqual({ packages: ["npm:existing"] });
});
});
describe("httpIdleTimeoutMs", () => {
@@ -1,6 +1,7 @@
import { mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import lockfile from "proper-lockfile";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { hasProjectConfig, ProjectTrustStore } from "../src/core/trust-manager.ts";
@@ -43,6 +44,29 @@ describe("ProjectTrustStore", () => {
expect(readFileSync(trustPath, "utf-8")).toBe("{not json");
});
it("does not read trust.json while another process holds the trust lock", () => {
const trustPath = join(agentDir, "trust.json");
writeFileSync(trustPath, "{partial", "utf-8");
const release = lockfile.lockSync(agentDir, { realpath: false, lockfilePath: `${trustPath}.lock` });
const store = new ProjectTrustStore(agentDir);
try {
let error: unknown;
try {
store.get(cwd);
} catch (caught) {
error = caught;
}
expect(error).toBeInstanceOf(Error);
expect((error as { code?: unknown }).code).toBe("ELOCKED");
} finally {
release();
}
expect(() => store.get(cwd)).toThrow(/Failed to read trust store/);
});
it("detects .pi project config directories", () => {
expect(hasProjectConfig(cwd)).toBe(false);