Git for the intent behind agent-written code. Records what your coding agent decided and why, and links every commit to it.
JavaScript ai-codingclaudeclaude-codeclaude-code-pluginclicoding-agentsdeveloper-toolsgitllmtypescript
feat(hooks): log what detached hook runs print
Eren6a5f41924dc7
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex b42a08e..5f2ad52 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).## [Unreleased]+### Added+- What the detached hook runs (the map build, the side model) print goes to `.cogit/state/detached.log` (local, never committed, starts over past 256 KB); a failed side run used to leave no trace.## [0.15.0] - 2026-10-27### Addeddiff --git a/packages/cli/src/hooks/run.ts b/packages/cli/src/hooks/run.tsindex 5181a39..9448634 100644--- a/packages/cli/src/hooks/run.ts+++ b/packages/cli/src/hooks/run.ts@@ -1,5 +1,6 @@import { spawn } from "node:child_process";-import { existsSync } from "node:fs";+import { closeSync, existsSync, mkdirSync, openSync, statSync, writeSync } from "node:fs";+import { dirname } from "node:path";import { fileURLToPath } from "node:url";import { type AgentAdapter, defaultAgent, type HookInput } from "../agents/index.js";import { Slug } from "../schema/common.js";@@ -33,7 +34,7 @@ export interface HookDeps {/** The CLI file a detached child runs; defaults to the running script. */cli?: string;/** Starts the detached process; tests observe instead of spawning. */- spawnDetached?: (cli: string, args: string[], env: NodeJS.ProcessEnv) => void;+ spawnDetached?: (cli: string, args: string[], env: NodeJS.ProcessEnv, log?: string) => void;}const HANDLERS: Record<string, (ctx: HookContext) => Promise<HookOutput | undefined>> = {@@ -46,16 +47,54 @@ const HANDLERS: Record<string, (ctx: HookContext) => Promise<HookOutput | undefiexport const HOOK_EVENTS = Object.keys(HANDLERS);+/** Past this size the log starts over, so a long-lived project never grows it unbounded. */+const LOG_LIMIT = 256 * 1024;++/**+ * Opens `log` for appending, with a header naming the run, and returns its fd;+ * undefined when it cannot be opened, so logging never stops the child.+ */+function openLog(log: string, args: string[]): number | undefined {+ try {+ mkdirSync(dirname(log), { recursive: true });+ let flag = "a";+ try {+ if (statSync(log).size > LOG_LIMIT) flag = "w";+ } catch {+ // No log yet.+ }+ const fd = openSync(log, flag);+ writeSync(fd, `\n--- ${new Date().toISOString()} ${args.join(" ")}\n`);+ return fd;+ } catch {+ return undefined;+ }+}+/*** Starts `node <cli> <args>` detached, with capture disabled in the child so it- * cannot fire these hooks again. The hook returns at once.+ * cannot fire these hooks again. The hook returns at once. With `log`, the+ * child's stdout and stderr are appended there; a failed side run is otherwise+ * invisible.*/-export function spawnDetachedProcess(cli: string, args: string[], env: NodeJS.ProcessEnv): void {- spawn(process.execPath, [cli, ...args], {- detached: true,- stdio: "ignore",- env: { ...env, COGIT_DISABLE: "1" },- }).unref();+export function spawnDetachedProcess(+ cli: string,+ args: string[],+ env: NodeJS.ProcessEnv,+ log?: string,+): void {+ const fd = log === undefined ? undefined : openLog(log, args);+ const out = fd ?? "ignore";+ try {+ spawn(process.execPath, [cli, ...args], {+ detached: true,+ stdio: ["ignore", out, out],+ env: { ...env, COGIT_DISABLE: "1" },+ }).unref();+ } finally {+ // The child holds its own copy of the descriptor.+ if (fd !== undefined) closeSync(fd);+ }}/**@@ -95,7 +134,7 @@ export async function runHook(if (!initialized && handler !== onSessionStart) return undefined;const cli = deps.cli ?? process.argv[1] ?? fileURLToPath(import.meta.url);const startCatchUp = (args: string[]) =>- (deps.spawnDetached ?? spawnDetachedProcess)(cli, args, env);+ (deps.spawnDetached ?? spawnDetachedProcess)(cli, args, env, projectPaths(root).detachedLog);const out = await handler({agent,input,diff --git a/packages/cli/src/store/paths.ts b/packages/cli/src/store/paths.tsindex a14c848..ffec6e4 100644--- a/packages/cli/src/store/paths.ts+++ b/packages/cli/src/store/paths.ts@@ -40,6 +40,8 @@ export function projectPaths(root: string) {readState: join(base, "state", "read.json"),/** Inbox rows the user dismissed, per session (Story 11.1); also local. */inboxState: join(base, "state", "inbox.json"),+ /** What the detached hook children (build, side run) printed; local, capped. */+ detachedLog: join(base, "state", "detached.log"),locks: join(base, "locks"),/** Where the skill drops session JSON for `session write --file`. */inbox: (id: string) => join(base, "inbox", `${id}.json`),diff --git a/packages/cli/test/hooks.test.ts b/packages/cli/test/hooks.test.tsindex c1c0357..25e493e 100644--- a/packages/cli/test/hooks.test.ts+++ b/packages/cli/test/hooks.test.ts@@ -1,6 +1,7 @@import { execFile } from "node:child_process";import { existsSync } from "node:fs";-import { mkdir, readFile, rm, writeFile } from "node:fs/promises";+import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";+import { tmpdir } from "node:os";import { join } from "node:path";import { promisify } from "node:util";import { beforeEach, describe, expect, it } from "vitest";@@ -582,6 +583,24 @@ describe("runHook", () => {expect(() => spawnDetachedProcess("/dev/null", [], env)).not.toThrow();});+ it("appends the detached child's output to the log, starting over once it is large", async () => {+ const dir = await mkdtemp(join(tmpdir(), "cogit-log-"));+ const script = join(dir, "child.mjs");+ await writeFile(script, 'console.log("out"); console.error("err");\n');+ const log = join(dir, "state", "detached.log");+ await mkdir(join(dir, "state"));+ await writeFile(log, "x".repeat(300 * 1024));+ spawnDetachedProcess(script, ["side", "run"], env, log);+ await expect.poll(() => readFile(log, "utf8"), { timeout: 5000 }).toMatch(/err\n/);+ const text = await readFile(log, "utf8");+ expect(text).not.toContain("xxx");+ expect(text).toMatch(/^\n--- \S+ side run\n/);+ expect(text).toContain("out\n");+ // A log that cannot be opened (its parent is a file) still starts the child.+ expect(() => spawnDetachedProcess("/dev/null", [], env, join(script, "log"))).not.toThrow();+ await rm(dir, { recursive: true, force: true });+ });+it("stays silent when disabled, for unknown events and for bad input", async () => {await initialized(1);expect(await stop({}, { ...env, COGIT_DISABLE: "1" })).toBeUndefined();