Skip to content

Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.

feat(trail): mark graft trail watch requests with X-Graft-Watch

anirudhkumar-nanonetsfe30ead39d5e
diff --git a/src/brain/watch-trail.ts b/src/brain/watch-trail.ts
index d2caff46..2ce2c45a 100644
--- a/src/brain/watch-trail.ts
+++ b/src/brain/watch-trail.ts
@@ -192,7 +192,11 @@ export async function watchTrail(repo: string, link: BrainLink, opts: WatchTrail
// waiting on, and an unref'd one would let it exit mid-watch with nothing
// printed — the same trap watch.ts documents for the build watcher.
const sleep = opts.sleep ?? ((ms: number) => new Promise<void>((r) => setTimeout(r, ms)));
- const fetchImpl = opts.fetchImpl ?? fetch;
+ // Every request carries X-Graft-Watch, which is how Trail knows something is
+ // watching this trail right now and can tell the page that Claude Code will
+ // ask before pulling, instead of telling the person to run the pull. A manual
+ // pull and the session-start check do not send it.
+ const fetchImpl = withWatchHeader(opts.fetchImpl ?? fetch);
const startedAt = now();
const deadline = startedAt + timeoutMs;
@@ -379,3 +383,12 @@ export function trailContextLine(s: TrailSnapshot, lastSeen?: number): string |
if (d <= 0) return null;
return `Trail: ${fmt(d)} new ${plural(d, "suggestion since your last session is", "suggestions since your last session are")} waiting for review at ${s.reviewUrl}.`;
}
+
+/** `fetch` with X-Graft-Watch: 1 added to every request's headers. */
+export function withWatchHeader(inner: typeof fetch): typeof fetch {
+ return ((input: Parameters<typeof fetch>[0], init?: RequestInit) => {
+ const headers = new Headers(init?.headers);
+ headers.set("x-graft-watch", "1");
+ return inner(input, { ...init, headers });
+ }) as typeof fetch;
+}
diff --git a/test/brain-trail-watch.test.ts b/test/brain-trail-watch.test.ts
index 1d4307ea..84d41743 100644
--- a/test/brain-trail-watch.test.ts
+++ b/test/brain-trail-watch.test.ts
@@ -18,6 +18,7 @@ import {
watchExitLines,
watchTrail,
type WatchTrailOptions,
+ withWatchHeader,
} from '../src/brain/watch-trail.js';
import type { BrainLink } from '../src/brain/link.js';
import { runCli, tmpRepo } from './helpers.js';
@@ -212,3 +213,20 @@ test('graft trail watch on a repo with no trail exits 1 at once, and --help docu
assert.match(help.stdout, /--accepted-only/);
assert.match(help.stdout, /exit 2/);
});
+
+// Trail tells the page that Claude Code is watching from this header; a watch
+// must send it on every read, and a manual pull must not (it uses gatherPull
+// with a plain fetch).
+test('withWatchHeader adds X-Graft-Watch to every request and keeps the rest', async () => {
+ const seen: Headers[] = [];
+ const inner = (async (_u: string, init?: RequestInit) => {
+ seen.push(new Headers(init?.headers));
+ return new Response('{}', { status: 200 });
+ }) as unknown as typeof fetch;
+ const f = withWatchHeader(inner);
+ await f('http://trail.test/a', { headers: { authorization: 'Bearer t', accept: 'application/json' } });
+ await f('http://trail.test/b');
+ assert.equal(seen[0]!.get('x-graft-watch'), '1');
+ assert.equal(seen[0]!.get('authorization'), 'Bearer t');
+ assert.equal(seen[1]!.get('x-graft-watch'), '1');
+});