An open-source, faster Devin alternative
TypeScript
Knowledge hooks: install only into the clone's own hooks directory, never where core.hooksPath points; unique temp names
Igor Zalutski via Computer015b6547bc43
diff --git a/computer/src/workspaces/knowledge-hooks.test.ts b/computer/src/workspaces/knowledge-hooks.test.tsindex 2a8242db..d1caeec7 100644--- a/computer/src/workspaces/knowledge-hooks.test.ts+++ b/computer/src/workspaces/knowledge-hooks.test.ts@@ -3,7 +3,7 @@// it (as fork.ts makes them), committing and pushing the way an agent does.import { spawnSync } from 'node:child_process';-import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs';+import { chmodSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs';import os from 'node:os';import path from 'node:path';import { afterAll, beforeAll, describe, expect, it } from 'vitest';@@ -99,7 +99,7 @@ describe('syncKnowledgeHooks', () => {await syncKnowledgeHooks(clone, knowledge, source);expect(readFileSync(file, 'utf8')).toBe(readFileSync(path.join(source, 'pre-push'), 'utf8'));expect(statSync(path.join(hooks, 'prepare-commit-msg')).mode & 0o111).toBe(0o111);- expect(existsSync(`${file}.installing`)).toBe(false);+ expect(readdirSync(hooks).filter((f) => f.includes('.installing'))).toEqual([]);});it("never replaces someone else's hook, and says so", async () => {@@ -116,6 +116,33 @@ describe('syncKnowledgeHooks', () => {expect(readFileSync(path.join(dir, '.git', 'hooks', 'pre-push'), 'utf8')).toBe(theirs);await syncKnowledgeHooks(path.join(root, 'nowhere'), plain, source); // nothing there: nothing to do});++ it('installs nothing where core.hooksPath points, global or the clone\'s own, and says so', async () => {+ const dir = path.join(root, 'hookspath');+ git(root, ['init', '-q', dir]);+ const shared = path.join(root, 'shared-hooks');+ const ours = () => [shared, path.join(dir, '.git', 'hooks'), path.join(dir, 'local-hooks')].flatMap((d) =>+ existsSync(d) ? readdirSync(d).filter((f) => !f.endsWith('.sample') && (KNOWLEDGE_HOOKS as readonly string[]).includes(f.split('.')[0]!)) : [],+ );+ const global = path.join(root, 'gitconfig');+ const before = readFileSync(global, 'utf8');+ try {+ writeFileSync(global, `${before}[core]\n\thooksPath = ${shared}\n`);+ configureAsRunner(undefined, env()); // the host's git sees that global configuration+ await expect(syncKnowledgeHooks(dir, knowledge, source)).rejects.toThrow(/core\.hooksPath .*shared-hooks.*not installed/);+ expect(ours()).toEqual([]);+ } finally {+ writeFileSync(global, before);+ configureAsRunner(undefined, process.env);+ }+ git(dir, ['config', 'core.hooksPath', 'local-hooks']);+ await expect(syncKnowledgeHooks(dir, knowledge, source)).rejects.toThrow(/core\.hooksPath .*local-hooks/);+ expect(ours()).toEqual([]);+ // Pointed back at its own hooks directory: installed as usual.+ git(dir, ['config', 'core.hooksPath', '.git/hooks']);+ await syncKnowledgeHooks(dir, knowledge, source);+ expect(readFileSync(path.join(dir, '.git', 'hooks', 'pre-push'), 'utf8')).toContain(HOOK_MARK);+ });});describe('prepare-commit-msg', () => {diff --git a/computer/src/workspaces/knowledge-hooks.ts b/computer/src/workspaces/knowledge-hooks.tsindex 12fa2946..22d8d0be 100644--- a/computer/src/workspaces/knowledge-hooks.ts+++ b/computer/src/workspaces/knowledge-hooks.ts@@ -14,8 +14,13 @@// - A clone that is not (or no longer) the knowledge repository has none of// ours: they are removed from its hooks directory, so no other repository's// pushes are checked.+// - Installed only into the common directory's hooks/: a core.hooksPath (the+// clone's, or a global one shared by every repository on the host) is+// reported as a problem and nothing is written there, so our hooks never+// land in a directory other repositories run.import { execFile } from 'node:child_process';+import { randomUUID } from 'node:crypto';import { readFile, stat } from 'node:fs/promises';import path from 'node:path';import { promisify } from 'node:util';@@ -51,8 +56,12 @@ export async function syncKnowledgeHooks(clone: string, repo: RepoDef, source =return;}// Where git looks for this clone and its worktrees: core.hooksPath if set, else the common directory's hooks/.- const c = command('git', ['rev-parse', '--git-path', 'hooks'], { GIT_TERMINAL_PROMPT: '0' });- const dir = path.resolve(clone, (await exec(c.cmd, c.args, { cwd: clone, env: c.env })).stdout.trim());+ const c = command('git', ['rev-parse', '--git-common-dir', '--git-path', 'hooks'], { GIT_TERMINAL_PROMPT: '0' });+ const [common, hooksPath] = (await exec(c.cmd, c.args, { cwd: clone, env: c.env })).stdout.split('\n').map((l) => l.trim());+ const dir = path.resolve(clone, hooksPath);+ const own = path.join(path.resolve(clone, common), 'hooks');+ if (dir !== own)+ throw new Error(`core.hooksPath points ${clone} at ${dir}, which other repositories may share; work-tracking hooks not installed`);await mkdir(dir, { recursive: true });const taken: string[] = [];for (const name of KNOWLEDGE_HOOKS) {@@ -64,7 +73,7 @@ export async function syncKnowledgeHooks(clone: string, repo: RepoDef, source =taken.push(name);continue;}- const tmp = `${dest}.installing`;+ const tmp = `${dest}.installing-${process.pid}-${randomUUID()}`;await writeFile(tmp, want);await chmod(tmp, 0o755);await rename(tmp, dest);