fix(coding-agent): harden project trust persistence

This commit is contained in:
Armin Ronacher
2026-06-03 08:30:45 +02:00
parent e4132d75d8
commit 85c052dba3
4 changed files with 104 additions and 17 deletions
@@ -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);