Skip to content

Commit ac58479

Browse files
wSedlacekclaude
andcommitted
feat(providers): Copilot can write review guides
Review guides need to read the checkout, so these sessions get read-only tools. Copilot runs commands it considers read-only without asking, including git diff --output, so a pre-tool hook enforces the limit on every call. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 694823e commit ac58479

5 files changed

Lines changed: 211 additions & 10 deletions

File tree

‎docs/user/providers-copilot.md‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,11 @@ Stop a running background agent from **Lineage** without ending the turn.
5050

5151
## Generated text
5252

53-
Copilot can write thread titles, commit messages, and branch names when you select it for text
54-
generation in **Settings**.
53+
Copilot can write thread titles, commit messages, branch names, pull request titles and
54+
descriptions, and review guides when you select it for text generation in **Settings**. It is not
55+
chosen automatically, because generated text uses your Copilot quota.
56+
57+
Review guides can only read the checkout and run `git diff`, `git log`, and `git show`.
5558

5659
## If Copilot reports it is not compatible
5760

‎packages/provider-copilot/src/server/copilotSdkRuntime.test.ts‎

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import type { ModelInfo, PermissionRequest, SessionEvent } from "@github/copilot-sdk";
1+
import type {
2+
CopilotClient,
3+
CopilotSession,
4+
ModelInfo,
5+
PermissionRequest,
6+
SessionEvent,
7+
} from "@github/copilot-sdk";
28
import { assert, it } from "@effect/vitest";
39
import * as Deferred from "effect/Deferred";
410
import * as Effect from "effect/Effect";
@@ -18,6 +24,8 @@ import {
1824
COPILOT_SDK_ROLLBACK_METHOD,
1925
COPILOT_SDK_STEER_METHOD,
2026
COPILOT_SDK_STOP_TASK_METHOD,
27+
copilotInvestigationDecision,
28+
copilotInvestigationToolDecision,
2129
copilotPermissionDecision,
2230
copilotSdkClosesAssistantMessage,
2331
copilotSdkForkBoundary,
@@ -436,6 +444,65 @@ it("keeps only remote MCP servers with their headers", () => {
436444
);
437445
});
438446

447+
it("lets an investigation run plain git reads and read the checkout, and nothing else", () => {
448+
const shell = (fullCommandText: string, hasWriteFileRedirection = false) =>
449+
({ kind: "shell", fullCommandText, hasWriteFileRedirection }) as PermissionRequest;
450+
const read = (path: string) => ({ kind: "read", path }) as PermissionRequest;
451+
const decide = (request: PermissionRequest) =>
452+
copilotInvestigationDecision(request, "/repo").kind;
453+
454+
for (const allowed of [
455+
shell("git diff --stat abc123...HEAD"),
456+
shell("git log --oneline --format=%H abc123..HEAD"),
457+
shell("git diff abc123...HEAD -- src/a.ts"),
458+
shell("git show HEAD:src/a.ts"),
459+
read("/repo"),
460+
read("/repo/src/a.ts"),
461+
read("src/a.ts"),
462+
]) {
463+
assert.strictEqual(decide(allowed), "approve-once");
464+
}
465+
for (const refused of [
466+
shell("git diff --output=/tmp/x"),
467+
shell("git diff --outp=/tmp/x"),
468+
shell("git diff -o /tmp/x"),
469+
shell("git diff --ext-diff"),
470+
shell("git log --textconv"),
471+
shell("git diff | sh"),
472+
shell("git diff; rm -rf ."),
473+
shell("git diff $(rm x)"),
474+
shell("git diff > out", true),
475+
shell("git checkout main"),
476+
shell("git -c core.pager=sh diff"),
477+
shell("rm -rf ."),
478+
read("/etc/passwd"),
479+
read("../secrets"),
480+
read("/repo/../secrets"),
481+
read("/repository/other"),
482+
{ kind: "write" } as PermissionRequest,
483+
{ kind: "url" } as PermissionRequest,
484+
{ kind: "mcp" } as PermissionRequest,
485+
]) {
486+
assert.strictEqual(decide(refused), "reject");
487+
}
488+
});
489+
490+
// Copilot skips the permission request for these, so the hook alone stops them.
491+
it("refuses tool calls Copilot would run without asking", () => {
492+
const decide = (toolName: string, toolArgs: unknown) =>
493+
copilotInvestigationToolDecision({ toolName, toolArgs }, "/repo").permissionDecision;
494+
495+
assert.strictEqual(decide("bash", { command: "git log --oneline a..HEAD" }), "allow");
496+
assert.strictEqual(decide("view", { path: "/repo/a.ts" }), "allow");
497+
assert.strictEqual(decide("grep", { pattern: "x" }), "allow");
498+
assert.strictEqual(decide("bash", { command: "git diff --output=leak.txt" }), "deny");
499+
assert.strictEqual(decide("bash", { command: "ls" }), "deny");
500+
assert.strictEqual(decide("bash", {}), "deny");
501+
assert.strictEqual(decide("grep", { pattern: "x", path: "/etc" }), "deny");
502+
assert.strictEqual(decide("view", { path: "/repo/../secrets" }), "deny");
503+
assert.strictEqual(decide("edit", { path: "/repo/a.ts" }), "deny");
504+
});
505+
439506
it("rejects a permission unless an allow option was selected", () => {
440507
const selected = (optionId: string) =>
441508
copilotPermissionDecision({ outcome: { outcome: "selected", optionId } });
@@ -718,6 +785,32 @@ it.effect("refuses to fork from a turn its session does not have", () =>
718785
}).pipe(Effect.scoped),
719786
);
720787

788+
it("limits only an investigation to read-only tools behind the hook", async () => {
789+
const configs: Array<Parameters<CopilotClient["createSession"]>[0]> = [];
790+
const client = {
791+
start: async () => undefined,
792+
createSession: async (config: (typeof configs)[number]) => {
793+
configs.push(config);
794+
return {
795+
sessionId: "text",
796+
sendAndWait: async () => undefined,
797+
disconnect: async () => undefined,
798+
} as unknown as CopilotSession;
799+
},
800+
deleteSession: async () => undefined,
801+
stop: async () => [],
802+
};
803+
const input = { cwd: "/repo", model: "m", prompt: "p", timeoutMs: 1 };
804+
805+
await generateCopilotText(client, input);
806+
await generateCopilotText(client, { ...input, investigate: true });
807+
808+
assert.deepEqual(configs[0]?.availableTools, []);
809+
assert.isUndefined(configs[0]?.hooks);
810+
assert.deepEqual(configs[1]?.availableTools, ["view", "grep", "glob", "bash"]);
811+
assert.isDefined(configs[1]?.hooks?.onPreToolUse);
812+
});
813+
721814
it("stops the client when a text generation session cannot start", async () => {
722815
let stopped = false;
723816
const generated = generateCopilotText(

‎packages/provider-copilot/src/server/copilotSdkRuntime.ts‎

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ export interface CopilotSdkClient {
185185
readonly model: string;
186186
readonly prompt: string;
187187
readonly timeoutMs: number;
188+
/** Lets the model read the checkout, limited by `copilotInvestigationToolDecision`. */
189+
readonly investigate?: boolean;
188190
}) => Promise<string>;
189191
readonly stop: () => Promise<unknown>;
190192
readonly forceStop: () => Promise<void>;
@@ -246,22 +248,86 @@ function wrapSession(session: Awaited<ReturnType<CopilotClient["createSession"]>
246248
} satisfies CopilotSdkSession;
247249
}
248250

251+
const INVESTIGATION_TOOLS = ["view", "grep", "glob", "bash"];
252+
const READ_ONLY_GIT_COMMAND = /^git (?:diff|log|show)(?: [\w./:@^~=+,%-]+)*$/;
253+
// Git accepts abbreviated long options, so `--outp=x` has to fail like `--output=x`.
254+
const UNSAFE_GIT_OPTION = /^(?:-o|--ou|--ex|--te)/;
255+
const INVESTIGATION_FEEDBACK =
256+
"Read-only: only plain git diff, git log, and git show run, without pipes, and only files in this checkout can be read.";
257+
258+
const isReadOnlyGitCommand = (command: string) =>
259+
READ_ONLY_GIT_COMMAND.test(command) &&
260+
!command.split(" ").some((word) => UNSAFE_GIT_OPTION.test(word));
261+
262+
/**
263+
* The enforcement point: Copilot runs commands it considers read-only, `git diff --output`
264+
* included, without a permission request, but this hook sees every tool call.
265+
*/
266+
export function copilotInvestigationToolDecision(
267+
input: { readonly toolName: string; readonly toolArgs: unknown },
268+
cwd: string,
269+
) {
270+
const args = record(input.toolArgs);
271+
const path = stringField(args, "path");
272+
const allowed =
273+
input.toolName === "bash"
274+
? isReadOnlyGitCommand(stringField(args, "command") ?? "")
275+
: INVESTIGATION_TOOLS.includes(input.toolName) &&
276+
(path === undefined || isInsideDirectory(cwd, path));
277+
return allowed
278+
? ({ permissionDecision: "allow" } as const)
279+
: ({ permissionDecision: "deny", permissionDecisionReason: INVESTIGATION_FEEDBACK } as const);
280+
}
281+
282+
/** Second line behind the hook, for anything that still asks. */
283+
export function copilotInvestigationDecision(
284+
request: PermissionRequest,
285+
cwd: string,
286+
): PermissionRequestResult {
287+
const allowed =
288+
request.kind === "read"
289+
? isInsideDirectory(cwd, request.resolvedPath ?? request.path)
290+
: request.kind === "shell" &&
291+
!request.hasWriteFileRedirection &&
292+
isReadOnlyGitCommand(request.fullCommandText);
293+
return allowed ? { kind: "approve-once" } : { kind: "reject", feedback: INVESTIGATION_FEEDBACK };
294+
}
295+
296+
function isInsideDirectory(directory: string, path: string) {
297+
const absolute = path.startsWith("/") ? path : `${directory}/${path}`;
298+
return (
299+
(absolute === directory || absolute.startsWith(`${directory}/`)) &&
300+
!absolute.split("/").includes("..")
301+
);
302+
}
303+
249304
export async function generateCopilotText(
250305
client: Pick<CopilotClient, "start" | "createSession" | "deleteSession" | "stop">,
251306
input: {
252307
readonly cwd: string;
253308
readonly model: string;
254309
readonly prompt: string;
255310
readonly timeoutMs: number;
311+
readonly investigate?: boolean;
256312
},
257313
) {
258314
try {
259315
await client.start();
260316
const session = await client.createSession({
261317
model: input.model,
262318
workingDirectory: input.cwd,
263-
availableTools: [],
264-
onPermissionRequest: () => ({ kind: "reject" }),
319+
availableTools: input.investigate === true ? INVESTIGATION_TOOLS : [],
320+
onPermissionRequest: (request) =>
321+
input.investigate === true
322+
? copilotInvestigationDecision(request, input.cwd)
323+
: { kind: "reject" },
324+
...(input.investigate === true
325+
? {
326+
hooks: {
327+
onPreToolUse: (tool) => copilotInvestigationToolDecision(tool, input.cwd),
328+
},
329+
}
330+
: {}),
265331
});
266332
try {
267333
return (await session.sendAndWait(input.prompt, input.timeoutMs))?.data.content ?? "";

‎packages/provider-copilot/src/server/textGeneration.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,44 @@ it.layer(NodeServices.layer)("GitHub Copilot text generation", (it) => {
4848
}),
4949
);
5050

51+
it.effect("gives a review guide read access and the investigation time limit", () =>
52+
Effect.gen(function* () {
53+
let sent: { readonly investigate?: boolean; readonly timeoutMs: number } | undefined;
54+
const generation = makeGitHubCopilotTextGeneration({
55+
settings,
56+
environment: {},
57+
fileSystem: yield* FileSystem.FileSystem,
58+
path: yield* Path.Path,
59+
makeClient: () =>
60+
({
61+
generateText: async (input: NonNullable<typeof sent>) => {
62+
sent = input;
63+
return '{"stops":[]}';
64+
},
65+
}) as unknown as CopilotSdkClient,
66+
});
67+
68+
yield* generation
69+
.generateReviewGuide({
70+
cwd: "/repo",
71+
number: 1,
72+
title: "t",
73+
description: "",
74+
baseBranch: "main",
75+
baseCommit: "abc",
76+
settledReview: "",
77+
modelSelection: {
78+
instanceId: ProviderInstanceId.make("githubCopilot"),
79+
model: "claude-haiku-4.5",
80+
},
81+
})
82+
.pipe(Effect.ignore);
83+
84+
assert.isTrue(sent?.investigate);
85+
assert.strictEqual(sent?.timeoutMs, 900_000);
86+
}),
87+
);
88+
5189
it.effect("force stops a CLI that never answers", () =>
5290
Effect.gen(function* () {
5391
let forced = false;

‎packages/provider-copilot/src/server/textGeneration.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
decodeJsonReply,
55
fromRunner,
66
type Request,
7-
withoutInvestigation,
87
} from "@t3tools/provider-core/server/textGenerationOperations";
98
import { resolveCommandPath } from "@t3tools/shared/shell";
109
import * as Effect from "effect/Effect";
@@ -17,7 +16,7 @@ import { makeCopilotSdkClient } from "./copilotSdkRuntime.ts";
1716

1817
const PROVIDER = "GitHub Copilot";
1918
const TIMEOUT_MS = 180_000;
20-
// `TIMEOUT_MS` only covers the reply, so a stalled CLI handshake needs its own limit.
19+
// The reply timeout does not cover startup, so a stalled CLI handshake needs its own limit.
2120
const STARTUP_TIMEOUT_MS = 30_000;
2221

2322
export function makeGitHubCopilotTextGeneration(input: {
@@ -41,13 +40,15 @@ export function makeGitHubCopilotTextGeneration(input: {
4140
});
4241
}
4342
const client = makeClient({ command, args: [], env: input.environment });
43+
const timeoutMs = request.investigate?.timeoutMs ?? TIMEOUT_MS;
4444
const reply = yield* Effect.tryPromise({
4545
try: () =>
4646
client.generateText({
4747
cwd: request.cwd,
4848
model: request.modelSelection.model,
4949
prompt: request.prompt,
50-
timeoutMs: TIMEOUT_MS,
50+
timeoutMs,
51+
investigate: request.investigate !== undefined,
5152
}),
5253
catch: (cause) =>
5354
new TextGenerationError({
@@ -69,7 +70,7 @@ export function makeGitHubCopilotTextGeneration(input: {
6970
}).pipe(Effect.ignore),
7071
),
7172
Effect.timeoutOrElse({
72-
duration: TIMEOUT_MS + STARTUP_TIMEOUT_MS,
73+
duration: timeoutMs + STARTUP_TIMEOUT_MS,
7374
orElse: () =>
7475
Effect.fail(
7576
new TextGenerationError({
@@ -81,5 +82,5 @@ export function makeGitHubCopilotTextGeneration(input: {
8182
);
8283
return yield* decodeJsonReply(request, PROVIDER, reply);
8384
});
84-
return fromRunner("GitHubCopilotTextGeneration", withoutInvestigation(PROVIDER, run));
85+
return fromRunner("GitHubCopilotTextGeneration", run);
8586
}

0 commit comments

Comments
 (0)