Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(daemon): don't cache a transient host-git-identity read failure

Claudettef7e7fdafac89
diff --git a/src/daemon/src/drivers/gitIdentityEnv.test.ts b/src/daemon/src/drivers/gitIdentityEnv.test.ts
index f2e42da02..091c6cbdb 100644
--- a/src/daemon/src/drivers/gitIdentityEnv.test.ts
+++ b/src/daemon/src/drivers/gitIdentityEnv.test.ts
@@ -1,5 +1,10 @@
-import { describe, it, expect } from "vitest";
-import { buildGitIdentityEnv } from "./gitIdentityEnv";
+import { describe, it, expect, vi, beforeEach } from "vitest";
+import { buildGitIdentityEnv, readHostGitIdentity, resetHostGitIdentityCache } from "./gitIdentityEnv";
+
+const mockExecFileSync = vi.fn();
+vi.mock("child_process", () => ({
+ execFileSync: (...args: unknown[]) => mockExecFileSync(...args),
+}));
describe("buildGitIdentityEnv", () => {
it("normal identity — plain name, [email protected] email, author===committer", () => {
@@ -112,3 +117,54 @@ describe("buildGitIdentityEnv", () => {
expect(env.GIT_AUTHOR_EMAIL).toBe("[email protected]");
});
});
+
+describe("readHostGitIdentity", () => {
+ beforeEach(() => {
+ resetHostGitIdentityCache();
+ mockExecFileSync.mockReset();
+ });
+
+ // Drive execFileSync("git", ["config","--get", <key>], …) by key.
+ const byKey = (map: Record<string, { value?: string; throw?: unknown }>) =>
+ (_git: string, args: string[]) => {
+ const key = args[args.length - 1];
+ const r = map[key] ?? {};
+ if ("throw" in r && r.throw !== undefined) throw r.throw;
+ return (r.value ?? "") + "\n";
+ };
+ const exit1 = () => Object.assign(new Error("exit 1"), { status: 1 }); // unset key
+ const timeout = () => Object.assign(new Error("ETIMEDOUT"), { /* no numeric status */ });
+
+ it("reads name + email from git config", () => {
+ mockExecFileSync.mockImplementation(byKey({ "user.name": { value: "Gus" }, "user.email": { value: "[email protected]" } }));
+ expect(readHostGitIdentity()).toEqual({ name: "Gus", email: "[email protected]" });
+ });
+
+ it("unset keys (clean exit-1) ⇒ null, and it's CACHED (definitive not-configured)", () => {
+ mockExecFileSync.mockImplementation(byKey({ "user.name": { throw: exit1() }, "user.email": { throw: exit1() } }));
+ expect(readHostGitIdentity()).toBeNull();
+ // Second call must NOT re-invoke git — a definitive unset is cached.
+ mockExecFileSync.mockClear();
+ expect(readHostGitIdentity()).toBeNull();
+ expect(mockExecFileSync).not.toHaveBeenCalled();
+ });
+
+ it("TRANSIENT failure (no numeric status) is NOT cached — next spawn re-reads and recovers", () => {
+ // First spawn: git times out on both reads → undefined, but transient → no cache.
+ mockExecFileSync.mockImplementation(byKey({ "user.name": { throw: timeout() }, "user.email": { throw: timeout() } }));
+ expect(readHostGitIdentity()).toBeNull();
+ // Second spawn: git answers → the owner identity is recovered (NOT poisoned
+ // by the first unlucky timeout). This is Blair's finding.
+ mockExecFileSync.mockImplementation(byKey({ "user.name": { value: "Gus" }, "user.email": { value: "[email protected]" } }));
+ expect(readHostGitIdentity()).toEqual({ name: "Gus", email: "[email protected]" });
+ });
+
+ it("one field transient ⇒ whole read treated as non-definitive (not cached)", () => {
+ mockExecFileSync.mockImplementation(byKey({ "user.name": { value: "Gus" }, "user.email": { throw: timeout() } }));
+ const first = readHostGitIdentity();
+ expect(first).toEqual({ name: "Gus", email: undefined });
+ // Not cached (email read was transient) → re-read picks up the now-available email.
+ mockExecFileSync.mockImplementation(byKey({ "user.name": { value: "Gus" }, "user.email": { value: "[email protected]" } }));
+ expect(readHostGitIdentity()).toEqual({ name: "Gus", email: "[email protected]" });
+ });
+});
diff --git a/src/daemon/src/drivers/gitIdentityEnv.ts b/src/daemon/src/drivers/gitIdentityEnv.ts
index 3b259ec9a..2b32b3636 100644
--- a/src/daemon/src/drivers/gitIdentityEnv.ts
+++ b/src/daemon/src/drivers/gitIdentityEnv.ts
@@ -144,24 +144,44 @@ let hostGitIdentityMemo: { value: HostGitUser | null } | undefined;
*/
export function readHostGitIdentity(): HostGitUser | null {
if (hostGitIdentityMemo) return hostGitIdentityMemo.value;
+
+ // `read` distinguishes THREE outcomes, because caching a transient failure
+ // as "no identity" would silently strip the owner's authorship for the whole
+ // daemon lifetime (Blair's finding):
+ // - a value → git ran, key set.
+ // - undefined → git ran cleanly but the key is UNSET (a definitive
+ // "not configured"; safe to cache).
+ // - transient=true → `git config` THREW (git missing / timed out / busy);
+ // NOT definitive — don't cache, retry next spawn.
+ let transient = false;
const read = (key: string): string | undefined => {
try {
+ // `--get` exits 1 (no throw with our stdio) when the key is unset, so a
+ // clean run with empty output = definitively unset, distinct from a throw.
const out = execFileSync("git", ["config", "--get", key], {
encoding: "utf8",
timeout: HOST_GIT_READ_TIMEOUT_MS,
stdio: ["ignore", "pipe", "ignore"],
}).trim();
return out.length > 0 ? out : undefined;
- } catch {
- // Unset key exits non-zero; git missing / timeout throws — all mean
- // "no host identity for this field", never fatal.
+ } catch (err) {
+ // Exit-1 (unset key) is reported by execFileSync as a throw too, so
+ // disambiguate: a non-zero EXIT (status is a number) = git ran, key
+ // unset = definitive. No numeric status (ENOENT / ETIMEDOUT / signal) =
+ // git couldn't answer = transient, must not be cached.
+ const status = (err as { status?: unknown } | undefined)?.status;
+ if (typeof status !== "number") transient = true;
return undefined;
}
};
+
const name = read("user.name");
const email = read("user.email");
const value = name === undefined && email === undefined ? null : { name, email };
- hostGitIdentityMemo = { value };
+ // Cache only a DEFINITIVE read. If any field's read threw a transient error,
+ // leave the memo empty so the next spawn re-reads (the owner's real identity
+ // isn't lost to one unlucky timeout at daemon start).
+ if (!transient) hostGitIdentityMemo = { value };
return value;
}