Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

feat(daemon): per-agent git identity for commit attribution

Gusc4530a98332c
diff --git a/src/daemon/src/daemon/createDaemon.ts b/src/daemon/src/daemon/createDaemon.ts
index a825d0195..d4a019b71 100644
--- a/src/daemon/src/daemon/createDaemon.ts
+++ b/src/daemon/src/daemon/createDaemon.ts
@@ -541,6 +541,7 @@ export async function createDaemon(opts: CreateDaemonOptions): Promise<RunningDa
agentCliPath: resolvedCliPath ?? opts.agentCliPath,
config: {
...(botMeta?.name ? { agentName: botMeta.name } : {}),
+ ...(botMeta?.discriminator ? { agentDiscriminator: botMeta.discriminator } : {}),
...(botMeta?.name && botMeta?.discriminator
? { agentHandle: `@${formatHandle(botMeta.name, botMeta.discriminator)}` }
: {}),
diff --git a/src/daemon/src/drivers/cliTransport.test.ts b/src/daemon/src/drivers/cliTransport.test.ts
index ed8a59db5..c45c23208 100644
--- a/src/daemon/src/drivers/cliTransport.test.ts
+++ b/src/daemon/src/drivers/cliTransport.test.ts
@@ -143,6 +143,36 @@ describe("prepareCliTransport — layered spawn env", () => {
expect(spawnEnv.NO_PROXY).toContain("localhost");
});
+ it("injects per-agent GIT_AUTHOR_*/GIT_COMMITTER_* from the agent identity", async () => {
+ const ctx = baseCtx(mkTmp(), {
+ agentId: "a3f90c21beef",
+ config: { agentName: "Melisa", agentDiscriminator: "1043" },
+ });
+ const { spawnEnv } = await prepareCliTransport(ctx, {}, undefined, "linux");
+ expect(spawnEnv.GIT_AUTHOR_NAME).toBe("Melisa#1043");
+ expect(spawnEnv.GIT_AUTHOR_EMAIL).toBe("[email protected]");
+ expect(spawnEnv.GIT_COMMITTER_NAME).toBe("Melisa#1043");
+ expect(spawnEnv.GIT_COMMITTER_EMAIL).toBe(spawnEnv.GIT_AUTHOR_EMAIL);
+ });
+
+ it("falls back to a valid generic git NAME when no agent name is set, email still keyed on agentId", async () => {
+ // config: {} (baseCtx default) — degraded/unknown-bot spawn. baseCtx has
+ // agentId "agent_1", so the email stays unique off the id even with no name.
+ const { spawnEnv } = await prepareCliTransport(baseCtx(mkTmp()), {}, undefined, "linux");
+ expect(spawnEnv.GIT_AUTHOR_NAME).toBe("Alook Agent");
+ expect(spawnEnv.GIT_AUTHOR_EMAIL).toBe("[email protected]");
+ expect(spawnEnv.GIT_AUTHOR_NAME).not.toBe("");
+ });
+
+ it("a driver trying to spoof GIT_AUTHOR_NAME does NOT win (gitIdentity precedence 35 > driver 30)", async () => {
+ const ctx = baseCtx(mkTmp(), {
+ agentId: "a3f90c21beef",
+ config: { agentName: "Melisa", agentDiscriminator: "1043" },
+ });
+ const { spawnEnv } = await prepareCliTransport(ctx, { GIT_AUTHOR_NAME: "Someone Else" }, undefined, "linux");
+ expect(spawnEnv.GIT_AUTHOR_NAME).toBe("Melisa#1043");
+ });
+
it("creates a symlink when hostCliPath is set", async () => {
const wd = mkTmp();
const host = path.join(wd, "real.js");
diff --git a/src/daemon/src/drivers/cliTransport.ts b/src/daemon/src/drivers/cliTransport.ts
index 121f2913c..0e23e89af 100644
--- a/src/daemon/src/drivers/cliTransport.ts
+++ b/src/daemon/src/drivers/cliTransport.ts
@@ -48,6 +48,7 @@ import { buildCliSystemPrompt } from "./systemPrompt.js";
import { resolveLaunchFieldsOrDefault } from "../runtimeConfig.js";
import { writeCliLink } from "./cliLink.js";
import { mergeEnvLayers, platformEnv, runtimeContextEnv, type EnvLayer } from "./spawnEnv.js";
+import { buildGitIdentityEnv } from "./gitIdentityEnv.js";
import { writeAgentFile } from "./agentFile.js";
export interface PreparedCliTransport {
@@ -200,6 +201,20 @@ export async function prepareCliTransport(
{ name: "hostStatic", precedence: 10, vars: cli.extraEnv ?? {} },
{ name: "userEnv", precedence: 20, vars: resolved.envVars },
{ name: "driver", precedence: 30, vars: extraEnv as Record<string, string | undefined> },
+ // Per-agent git identity (GIT_AUTHOR_*/GIT_COMMITTER_*) so commits are
+ // attributed to the agent that made them. ENV-ONLY by design — never writes
+ // ~/.gitconfig or any repo .git/config, so the owner's own git identity is
+ // untouched. Above `driver` (30) so a driver can't clobber it; below
+ // `platformContract` (40) though keys don't overlap. Not sensitive.
+ {
+ name: "gitIdentity",
+ precedence: 35,
+ vars: buildGitIdentityEnv({
+ agentName: ctx.config.agentName,
+ discriminator: ctx.config.agentDiscriminator,
+ agentId: ctx.agentId,
+ }),
+ },
{
name: "platformContract",
precedence: 40,
diff --git a/src/daemon/src/drivers/gitIdentityEnv.test.ts b/src/daemon/src/drivers/gitIdentityEnv.test.ts
new file mode 100644
index 000000000..ad8b59575
--- /dev/null
+++ b/src/daemon/src/drivers/gitIdentityEnv.test.ts
@@ -0,0 +1,57 @@
+import { describe, it, expect } from "vitest";
+import { buildGitIdentityEnv } from "./gitIdentityEnv";
+
+describe("buildGitIdentityEnv", () => {
+ it("normal identity — name#disc, unique email, author===committer", () => {
+ const env = buildGitIdentityEnv({ agentName: "Melisa", discriminator: "1043", agentId: "a3f90c21beef" });
+ expect(env.GIT_AUTHOR_NAME).toBe("Melisa#1043");
+ expect(env.GIT_AUTHOR_EMAIL).toBe("[email protected]");
+ expect(env.GIT_COMMITTER_NAME).toBe(env.GIT_AUTHOR_NAME);
+ expect(env.GIT_COMMITTER_EMAIL).toBe(env.GIT_AUTHOR_EMAIL);
+ });
+
+ it("name with spaces/punct — email local-part is ASCII-slugged, display name kept", () => {
+ const env = buildGitIdentityEnv({ agentName: "Bot One!", discriminator: "0007", agentId: "zzzz1111" });
+ expect(env.GIT_AUTHOR_NAME).toBe("Bot One!#0007");
+ expect(env.GIT_AUTHOR_EMAIL).toBe("[email protected]");
+ });
+
+ it("CJK/emoji name — empty ASCII slug is dropped, email falls back to disc+shortid, display keeps UTF-8", () => {
+ const env = buildGitIdentityEnv({ agentName: "总部🎉", discriminator: "1043", agentId: "a3f90c21beef" });
+ expect(env.GIT_AUTHOR_NAME).toBe("总部🎉#1043");
+ expect(env.GIT_AUTHOR_EMAIL).toBe("[email protected]");
+ });
+
+ it("strips control chars / newlines from the name (guards commit-metadata injection)", () => {
+ const env = buildGitIdentityEnv({
+ agentName: "Evil\r\nBot\x00 X",
+ discriminator: "1234",
+ agentId: "deadbeef00",
+ });
+ // Control chars removed; the surviving space between "Bot" and "X" stays.
+ expect(env.GIT_AUTHOR_NAME).toBe("EvilBot X#1234");
+ expect(env.GIT_AUTHOR_NAME).not.toMatch(/[\n\r\x00]/);
+ });
+
+ it("missing identity — falls back to a valid generic identity, never empty", () => {
+ const env = buildGitIdentityEnv({});
+ expect(env.GIT_AUTHOR_NAME).toBe("Alook Agent");
+ expect(env.GIT_AUTHOR_EMAIL).toBe("[email protected]");
+ for (const v of Object.values(env)) expect(v).not.toBe("");
+ });
+
+ it("UNIQUENESS — same name AND same discriminator but different agentId ⇒ DIFFERENT emails", () => {
+ const a = buildGitIdentityEnv({ agentName: "Twin", discriminator: "1043", agentId: "aaaaaaaa1111" });
+ const b = buildGitIdentityEnv({ agentName: "Twin", discriminator: "1043", agentId: "bbbbbbbb2222" });
+ expect(a.GIT_AUTHOR_NAME).toBe(b.GIT_AUTHOR_NAME); // display collides — that's fine
+ expect(a.GIT_AUTHOR_EMAIL).not.toBe(b.GIT_AUTHOR_EMAIL); // machine-readable key must not
+ expect(a.GIT_AUTHOR_EMAIL).toBe("[email protected]");
+ expect(b.GIT_AUTHOR_EMAIL).toBe("[email protected]");
+ });
+
+ it("agentId is ASCII-slugged before slicing (nanoid '_'/'-' stay email-safe)", () => {
+ const env = buildGitIdentityEnv({ agentName: "X", discriminator: "0001", agentId: "Ab_9-Cd_ef" });
+ // local-part contains only [a-z0-9-]; no stray underscores from the id.
+ expect(env.GIT_AUTHOR_EMAIL).toMatch(/^[a-z0-9-]+@alook\.ai$/);
+ });
+});
diff --git a/src/daemon/src/drivers/gitIdentityEnv.ts b/src/daemon/src/drivers/gitIdentityEnv.ts
new file mode 100644
index 000000000..57d5718cc
--- /dev/null
+++ b/src/daemon/src/drivers/gitIdentityEnv.ts
@@ -0,0 +1,91 @@
+/**
+ * Per-agent git identity for the spawn environment.
+ *
+ * The daemon injects `GIT_AUTHOR_*` / `GIT_COMMITTER_*` into each agent's child
+ * process so a commit is attributed to the agent that made it. This is
+ * ENV-ONLY: it never writes `~/.gitconfig` or any repo's `.git/config`, so the
+ * human owner's own git identity is never touched — the injection is effective
+ * only inside the agent's session.
+ *
+ * Both author and committer are set: git falls back committer → machine
+ * identity otherwise, which would leave attribution half-wrong.
+ *
+ * The EMAIL is the machine-readable attribution key and MUST be unique per
+ * agent. The discriminator alone is not enough (it is FNV-1a(user.id) mod
+ * 10000 — a 4-digit space with expected collisions), and the display-name slug
+ * is lossy ("Bot One"/"Bot-One" → "bot-one"). Uniqueness therefore comes from
+ * `agentId` (a nanoid): a slice of it suffixes the local-part so two distinct
+ * agents can never share an email even when name and discriminator collide.
+ * The NAME field stays the human `Name#Disc` — display only, so cosmetic
+ * ambiguity there is acceptable.
+ */
+
+const GIT_IDENTITY_DOMAIN = "alook.ai";
+const SHORT_ID_LEN = 8;
+
+/** Generic identity when an agent's name/handle is unavailable (degraded spawn). */
+const FALLBACK_NAME = "Alook Agent";
+const FALLBACK_LOCAL_PART = "alook-agent";
+
+/**
+ * Strip characters illegal in a git author/committer NAME. The name is a single
+ * header line, so CR/LF and other control chars must go (they could otherwise
+ * inject/split commit metadata). UTF-8 (CJK, emoji, …) is legal in a git name
+ * and is preserved. Whitespace is collapsed and trimmed. Returns "" if nothing
+ * legible remains.
+ */
+function sanitizeGitName(name: string): string {
+ // eslint-disable-next-line no-control-regex
+ return name.replace(/[\x00-\x1f\x7f]/g, "").replace(/\s+/g, " ").trim();
+}
+
+/**
+ * Reduce a string to a safe email local-part fragment: lowercase ASCII
+ * alphanumerics, every other run collapsed to a single "-", trimmed. Returns ""
+ * when nothing ASCII-alphanumeric survives (e.g. an all-CJK/emoji name) — the
+ * caller drops the empty fragment rather than emit a stray "-".
+ */
+function asciiSlug(s: string): string {
+ return s
+ .toLowerCase()
+ .replace(/[^a-z0-9]+/g, "-")
+ .replace(/^-+|-+$/g, "");
+}
+
+export interface GitIdentityInput {
+ agentName?: string;
+ discriminator?: string;
+ agentId?: string;
+}
+
+/**
+ * Build the four `GIT_*` env vars for one agent. Pure — no I/O. Always returns
+ * a valid, non-empty identity; falls back to a generic one when name is
+ * missing.
+ */
+export function buildGitIdentityEnv(input: GitIdentityInput): Record<string, string> {
+ const { agentName, discriminator, agentId } = input;
+
+ const cleanName = agentName ? sanitizeGitName(agentName) : "";
+ const displayName = cleanName
+ ? discriminator
+ ? `${cleanName}#${discriminator}`
+ : cleanName
+ : FALLBACK_NAME;
+
+ // Local-part fragments in readability order: name slug, discriminator, and a
+ // short slice of the (globally unique) agentId. The agentId slice is what
+ // guarantees per-agent uniqueness; the rest is for human readability.
+ const shortId = agentId ? asciiSlug(agentId).slice(0, SHORT_ID_LEN) : "";
+ const nameSlug = cleanName ? asciiSlug(cleanName) : "";
+ const localPart =
+ [nameSlug, discriminator, shortId].filter(Boolean).join("-") || FALLBACK_LOCAL_PART;
+ const email = `${localPart}@${GIT_IDENTITY_DOMAIN}`;
+
+ return {
+ GIT_AUTHOR_NAME: displayName,
+ GIT_AUTHOR_EMAIL: email,
+ GIT_COMMITTER_NAME: displayName,
+ GIT_COMMITTER_EMAIL: email,
+ };
+}
diff --git a/src/daemon/src/types.ts b/src/daemon/src/types.ts
index b8391f4b3..232332dd7 100644
--- a/src/daemon/src/types.ts
+++ b/src/daemon/src/types.ts
@@ -215,6 +215,12 @@ export interface LaunchConfig {
runtimeContext?: RuntimeContext;
/** Agent display name (e.g. "Gus"). */
agentName?: string;
+ /**
+ * Agent's 4-digit discriminator (the `0042` in `@Gus#4821`). Carried
+ * explicitly alongside `agentName` so the git-identity builder doesn't have
+ * to re-parse it out of `agentHandle`. See `drivers/gitIdentityEnv.ts`.
+ */
+ agentDiscriminator?: string;
/**
* Agent's global @mention handle, `@name#0042` (e.g. "@Gus#4821"). Every
* account in Alook — human or agent — has a name plus a 4-digit