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" ;
28import { assert , it } from "@effect/vitest" ;
39import * as Deferred from "effect/Deferred" ;
410import * 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+
439506it ( "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+
721814it ( "stops the client when a text generation session cannot start" , async ( ) => {
722815 let stopped = false ;
723816 const generated = generateCopilotText (
0 commit comments